nouveau_chan.c revision 1.2.30.1 1 /* $NetBSD: nouveau_chan.c,v 1.2.30.1 2018/09/06 06:56:18 pgoyette Exp $ */
2
3 /*
4 * Copyright 2012 Red Hat Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors: Ben Skeggs
25 */
26
27 #include <sys/cdefs.h>
28 __KERNEL_RCSID(0, "$NetBSD: nouveau_chan.c,v 1.2.30.1 2018/09/06 06:56:18 pgoyette Exp $");
29
30 #include <nvif/os.h>
31 #include <nvif/class.h>
32 #include <nvif/ioctl.h>
33
34 /*XXX*/
35 #include <core/client.h>
36
37 #include "nouveau_drm.h"
38 #include "nouveau_dma.h"
39 #include "nouveau_bo.h"
40 #include "nouveau_chan.h"
41 #include "nouveau_fence.h"
42 #include "nouveau_abi16.h"
43
44 MODULE_PARM_DESC(vram_pushbuf, "Create DMA push buffers in VRAM");
45 int nouveau_vram_pushbuf;
46 module_param_named(vram_pushbuf, nouveau_vram_pushbuf, int, 0400);
47
48 int
49 nouveau_channel_idle(struct nouveau_channel *chan)
50 {
51 if (likely(chan && chan->fence)) {
52 struct nouveau_cli *cli = (void *)chan->user.client;
53 struct nouveau_fence *fence = NULL;
54 int ret;
55
56 ret = nouveau_fence_new(chan, false, &fence);
57 if (!ret) {
58 ret = nouveau_fence_wait(fence, false, false);
59 nouveau_fence_unref(&fence);
60 }
61
62 if (ret) {
63 NV_PRINTK(err, cli, "failed to idle channel %d [%s]\n",
64 chan->chid, nvxx_client(&cli->base)->name);
65 return ret;
66 }
67 }
68 return 0;
69 }
70
71 void
72 nouveau_channel_del(struct nouveau_channel **pchan)
73 {
74 struct nouveau_channel *chan = *pchan;
75 if (chan) {
76 if (chan->fence)
77 nouveau_fence(chan->drm)->context_del(chan);
78 nvif_object_fini(&chan->nvsw);
79 nvif_object_fini(&chan->gart);
80 nvif_object_fini(&chan->vram);
81 nvif_object_fini(&chan->user);
82 nvif_object_fini(&chan->push.ctxdma);
83 nouveau_bo_vma_del(chan->push.buffer, &chan->push.vma);
84 nouveau_bo_unmap(chan->push.buffer);
85 if (chan->push.buffer && chan->push.buffer->pin_refcnt)
86 nouveau_bo_unpin(chan->push.buffer);
87 nouveau_bo_ref(NULL, &chan->push.buffer);
88 kfree(chan);
89 }
90 *pchan = NULL;
91 }
92
93 static int
94 nouveau_channel_prep(struct nouveau_drm *drm, struct nvif_device *device,
95 u32 size, struct nouveau_channel **pchan)
96 {
97 struct nouveau_cli *cli = (void *)device->object.client;
98 struct nvkm_mmu *mmu = nvxx_mmu(device);
99 static const struct nv_dma_v0 zero_args;
100 struct nv_dma_v0 args = zero_args;
101 struct nouveau_channel *chan;
102 u32 target;
103 int ret;
104
105 chan = *pchan = kzalloc(sizeof(*chan), GFP_KERNEL);
106 if (!chan)
107 return -ENOMEM;
108
109 chan->device = device;
110 chan->drm = drm;
111
112 /* allocate memory for dma push buffer */
113 target = TTM_PL_FLAG_TT | TTM_PL_FLAG_UNCACHED;
114 if (nouveau_vram_pushbuf)
115 target = TTM_PL_FLAG_VRAM;
116
117 ret = nouveau_bo_new(drm->dev, size, 0, target, 0, 0, NULL, NULL,
118 &chan->push.buffer);
119 if (ret == 0) {
120 ret = nouveau_bo_pin(chan->push.buffer, target, false);
121 if (ret == 0)
122 ret = nouveau_bo_map(chan->push.buffer);
123 }
124
125 if (ret) {
126 nouveau_channel_del(pchan);
127 return ret;
128 }
129
130 /* create dma object covering the *entire* memory space that the
131 * pushbuf lives in, this is because the GEM code requires that
132 * we be able to call out to other (indirect) push buffers
133 */
134 chan->push.vma.offset = chan->push.buffer->bo.offset;
135
136 if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
137 ret = nouveau_bo_vma_add(chan->push.buffer, cli->vm,
138 &chan->push.vma);
139 if (ret) {
140 nouveau_channel_del(pchan);
141 return ret;
142 }
143
144 args.target = NV_DMA_V0_TARGET_VM;
145 args.access = NV_DMA_V0_ACCESS_VM;
146 args.start = 0;
147 args.limit = cli->vm->mmu->limit - 1;
148 } else
149 if (chan->push.buffer->bo.mem.mem_type == TTM_PL_VRAM) {
150 if (device->info.family == NV_DEVICE_INFO_V0_TNT) {
151 /* nv04 vram pushbuf hack, retarget to its location in
152 * the framebuffer bar rather than direct vram access..
153 * nfi why this exists, it came from the -nv ddx.
154 */
155 args.target = NV_DMA_V0_TARGET_PCI;
156 args.access = NV_DMA_V0_ACCESS_RDWR;
157 args.start = nvxx_device(device)->func->
158 resource_addr(nvxx_device(device), 1);
159 args.limit = args.start + device->info.ram_user - 1;
160 } else {
161 args.target = NV_DMA_V0_TARGET_VRAM;
162 args.access = NV_DMA_V0_ACCESS_RDWR;
163 args.start = 0;
164 args.limit = device->info.ram_user - 1;
165 }
166 } else {
167 if (chan->drm->agp.bridge) {
168 args.target = NV_DMA_V0_TARGET_AGP;
169 args.access = NV_DMA_V0_ACCESS_RDWR;
170 args.start = chan->drm->agp.base;
171 args.limit = chan->drm->agp.base +
172 chan->drm->agp.size - 1;
173 } else {
174 args.target = NV_DMA_V0_TARGET_VM;
175 args.access = NV_DMA_V0_ACCESS_RDWR;
176 args.start = 0;
177 args.limit = mmu->limit - 1;
178 }
179 }
180
181 ret = nvif_object_init(&device->object, 0, NV_DMA_FROM_MEMORY,
182 &args, sizeof(args), &chan->push.ctxdma);
183 if (ret) {
184 nouveau_channel_del(pchan);
185 return ret;
186 }
187
188 return 0;
189 }
190
191 static int
192 nouveau_channel_ind(struct nouveau_drm *drm, struct nvif_device *device,
193 u32 engine, struct nouveau_channel **pchan)
194 {
195 static const u16 oclasses[] = { MAXWELL_CHANNEL_GPFIFO_A,
196 KEPLER_CHANNEL_GPFIFO_A,
197 FERMI_CHANNEL_GPFIFO,
198 G82_CHANNEL_GPFIFO,
199 NV50_CHANNEL_GPFIFO,
200 0 };
201 const u16 *oclass = oclasses;
202 union {
203 struct nv50_channel_gpfifo_v0 nv50;
204 struct fermi_channel_gpfifo_v0 fermi;
205 struct kepler_channel_gpfifo_a_v0 kepler;
206 } args;
207 struct nouveau_channel *chan;
208 u32 size;
209 int ret;
210
211 /* allocate dma push buffer */
212 ret = nouveau_channel_prep(drm, device, 0x12000, &chan);
213 *pchan = chan;
214 if (ret)
215 return ret;
216
217 /* create channel object */
218 do {
219 if (oclass[0] >= KEPLER_CHANNEL_GPFIFO_A) {
220 args.kepler.version = 0;
221 args.kepler.engine = engine;
222 args.kepler.ilength = 0x02000;
223 args.kepler.ioffset = 0x10000 + chan->push.vma.offset;
224 args.kepler.vm = 0;
225 size = sizeof(args.kepler);
226 } else
227 if (oclass[0] >= FERMI_CHANNEL_GPFIFO) {
228 args.fermi.version = 0;
229 args.fermi.ilength = 0x02000;
230 args.fermi.ioffset = 0x10000 + chan->push.vma.offset;
231 args.fermi.vm = 0;
232 size = sizeof(args.fermi);
233 } else {
234 args.nv50.version = 0;
235 args.nv50.ilength = 0x02000;
236 args.nv50.ioffset = 0x10000 + chan->push.vma.offset;
237 args.nv50.pushbuf = nvif_handle(&chan->push.ctxdma);
238 args.nv50.vm = 0;
239 size = sizeof(args.nv50);
240 }
241
242 ret = nvif_object_init(&device->object, 0, *oclass++,
243 &args, size, &chan->user);
244 if (ret == 0) {
245 if (chan->user.oclass >= KEPLER_CHANNEL_GPFIFO_A)
246 chan->chid = args.kepler.chid;
247 else
248 if (chan->user.oclass >= FERMI_CHANNEL_GPFIFO)
249 chan->chid = args.fermi.chid;
250 else
251 chan->chid = args.nv50.chid;
252 return ret;
253 }
254 } while (*oclass);
255
256 nouveau_channel_del(pchan);
257 return ret;
258 }
259
260 static int
261 nouveau_channel_dma(struct nouveau_drm *drm, struct nvif_device *device,
262 struct nouveau_channel **pchan)
263 {
264 static const u16 oclasses[] = { NV40_CHANNEL_DMA,
265 NV17_CHANNEL_DMA,
266 NV10_CHANNEL_DMA,
267 NV03_CHANNEL_DMA,
268 0 };
269 const u16 *oclass = oclasses;
270 struct nv03_channel_dma_v0 args;
271 struct nouveau_channel *chan;
272 int ret;
273
274 /* allocate dma push buffer */
275 ret = nouveau_channel_prep(drm, device, 0x10000, &chan);
276 *pchan = chan;
277 if (ret)
278 return ret;
279
280 /* create channel object */
281 args.version = 0;
282 args.pushbuf = nvif_handle(&chan->push.ctxdma);
283 args.offset = chan->push.vma.offset;
284
285 do {
286 ret = nvif_object_init(&device->object, 0, *oclass++,
287 &args, sizeof(args), &chan->user);
288 if (ret == 0) {
289 chan->chid = args.chid;
290 return ret;
291 }
292 } while (ret && *oclass);
293
294 nouveau_channel_del(pchan);
295 return ret;
296 }
297
298 static int
299 nouveau_channel_init(struct nouveau_channel *chan, u32 vram, u32 gart)
300 {
301 struct nvif_device *device = chan->device;
302 struct nouveau_cli *cli = (void *)chan->user.client;
303 struct nvkm_mmu *mmu = nvxx_mmu(device);
304 static const struct nv_dma_v0 zero_args;
305 struct nv_dma_v0 args = zero_args;
306 int ret, i;
307
308 ret = nvif_object_map(&chan->user);
309 if (ret) {
310 NV_PRINTK(err, cli, "nvif_object_map, %d\n", ret);
311 return ret;
312 }
313
314 /* allocate dma objects to cover all allowed vram, and gart */
315 if (device->info.family < NV_DEVICE_INFO_V0_FERMI) {
316 if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
317 args.target = NV_DMA_V0_TARGET_VM;
318 args.access = NV_DMA_V0_ACCESS_VM;
319 args.start = 0;
320 args.limit = cli->vm->mmu->limit - 1;
321 } else {
322 args.target = NV_DMA_V0_TARGET_VRAM;
323 args.access = NV_DMA_V0_ACCESS_RDWR;
324 args.start = 0;
325 args.limit = device->info.ram_user - 1;
326 }
327
328 ret = nvif_object_init(&chan->user, vram, NV_DMA_IN_MEMORY,
329 &args, sizeof(args), &chan->vram);
330 if (ret)
331 return ret;
332
333 if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
334 args.target = NV_DMA_V0_TARGET_VM;
335 args.access = NV_DMA_V0_ACCESS_VM;
336 args.start = 0;
337 args.limit = cli->vm->mmu->limit - 1;
338 } else
339 if (chan->drm->agp.bridge) {
340 args.target = NV_DMA_V0_TARGET_AGP;
341 args.access = NV_DMA_V0_ACCESS_RDWR;
342 args.start = chan->drm->agp.base;
343 args.limit = chan->drm->agp.base +
344 chan->drm->agp.size - 1;
345 } else {
346 args.target = NV_DMA_V0_TARGET_VM;
347 args.access = NV_DMA_V0_ACCESS_RDWR;
348 args.start = 0;
349 args.limit = mmu->limit - 1;
350 }
351
352 ret = nvif_object_init(&chan->user, gart, NV_DMA_IN_MEMORY,
353 &args, sizeof(args), &chan->gart);
354 if (ret)
355 return ret;
356 }
357
358 /* initialise dma tracking parameters */
359 switch (chan->user.oclass & 0x00ff) {
360 case 0x006b:
361 case 0x006e:
362 chan->user_put = 0x40;
363 chan->user_get = 0x44;
364 chan->dma.max = (0x10000 / 4) - 2;
365 break;
366 default:
367 chan->user_put = 0x40;
368 chan->user_get = 0x44;
369 chan->user_get_hi = 0x60;
370 chan->dma.ib_base = 0x10000 / 4;
371 chan->dma.ib_max = (0x02000 / 8) - 1;
372 chan->dma.ib_put = 0;
373 chan->dma.ib_free = chan->dma.ib_max - chan->dma.ib_put;
374 chan->dma.max = chan->dma.ib_base;
375 break;
376 }
377
378 chan->dma.put = 0;
379 chan->dma.cur = chan->dma.put;
380 chan->dma.free = chan->dma.max - chan->dma.cur;
381
382 ret = RING_SPACE(chan, NOUVEAU_DMA_SKIPS);
383 if (ret)
384 return ret;
385
386 for (i = 0; i < NOUVEAU_DMA_SKIPS; i++)
387 OUT_RING(chan, 0x00000000);
388
389 /* allocate software object class (used for fences on <= nv05) */
390 if (device->info.family < NV_DEVICE_INFO_V0_CELSIUS) {
391 ret = nvif_object_init(&chan->user, 0x006e,
392 NVIF_IOCTL_NEW_V0_SW_NV04,
393 NULL, 0, &chan->nvsw);
394 if (ret)
395 return ret;
396
397 ret = RING_SPACE(chan, 2);
398 if (ret)
399 return ret;
400
401 BEGIN_NV04(chan, NvSubSw, 0x0000, 1);
402 OUT_RING (chan, chan->nvsw.handle);
403 FIRE_RING (chan);
404 }
405
406 /* initialise synchronisation */
407 return nouveau_fence(chan->drm)->context_new(chan);
408 }
409
410 int
411 nouveau_channel_new(struct nouveau_drm *drm, struct nvif_device *device,
412 u32 arg0, u32 arg1, struct nouveau_channel **pchan)
413 {
414 struct nouveau_cli *cli = (void *)device->object.client;
415 bool super;
416 int ret;
417
418 /* hack until fencenv50 is fixed, and agp access relaxed */
419 super = cli->base.super;
420 cli->base.super = true;
421
422 ret = nouveau_channel_ind(drm, device, arg0, pchan);
423 if (ret) {
424 NV_PRINTK(dbg, cli, "ib channel create, %d\n", ret);
425 ret = nouveau_channel_dma(drm, device, pchan);
426 if (ret) {
427 NV_PRINTK(dbg, cli, "dma channel create, %d\n", ret);
428 goto done;
429 }
430 }
431
432 ret = nouveau_channel_init(*pchan, arg0, arg1);
433 if (ret) {
434 NV_PRINTK(err, cli, "channel failed to initialise, %d\n", ret);
435 nouveau_channel_del(pchan);
436 }
437
438 done:
439 cli->base.super = super;
440 return ret;
441 }
442