nouveau_chan.c revision 1.2.32.2 1 /* $NetBSD: nouveau_chan.c,v 1.2.32.2 2020/04/08 14:08:24 martin 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.32.2 2020/04/08 14:08:24 martin 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 struct nv_dma_v0 args = {};
100 struct nouveau_channel *chan;
101 u32 target;
102 int ret;
103
104 chan = *pchan = kzalloc(sizeof(*chan), GFP_KERNEL);
105 if (!chan)
106 return -ENOMEM;
107
108 chan->device = device;
109 chan->drm = drm;
110
111 /* allocate memory for dma push buffer */
112 target = TTM_PL_FLAG_TT | TTM_PL_FLAG_UNCACHED;
113 if (nouveau_vram_pushbuf)
114 target = TTM_PL_FLAG_VRAM;
115
116 ret = nouveau_bo_new(drm->dev, size, 0, target, 0, 0, NULL, NULL,
117 &chan->push.buffer);
118 if (ret == 0) {
119 ret = nouveau_bo_pin(chan->push.buffer, target, false);
120 if (ret == 0)
121 ret = nouveau_bo_map(chan->push.buffer);
122 }
123
124 if (ret) {
125 nouveau_channel_del(pchan);
126 return ret;
127 }
128
129 /* create dma object covering the *entire* memory space that the
130 * pushbuf lives in, this is because the GEM code requires that
131 * we be able to call out to other (indirect) push buffers
132 */
133 chan->push.vma.offset = chan->push.buffer->bo.offset;
134
135 if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
136 ret = nouveau_bo_vma_add(chan->push.buffer, cli->vm,
137 &chan->push.vma);
138 if (ret) {
139 nouveau_channel_del(pchan);
140 return ret;
141 }
142
143 args.target = NV_DMA_V0_TARGET_VM;
144 args.access = NV_DMA_V0_ACCESS_VM;
145 args.start = 0;
146 args.limit = cli->vm->mmu->limit - 1;
147 } else
148 if (chan->push.buffer->bo.mem.mem_type == TTM_PL_VRAM) {
149 if (device->info.family == NV_DEVICE_INFO_V0_TNT) {
150 /* nv04 vram pushbuf hack, retarget to its location in
151 * the framebuffer bar rather than direct vram access..
152 * nfi why this exists, it came from the -nv ddx.
153 */
154 args.target = NV_DMA_V0_TARGET_PCI;
155 args.access = NV_DMA_V0_ACCESS_RDWR;
156 args.start = nvxx_device(device)->func->
157 resource_addr(nvxx_device(device), 1);
158 args.limit = args.start + device->info.ram_user - 1;
159 } else {
160 args.target = NV_DMA_V0_TARGET_VRAM;
161 args.access = NV_DMA_V0_ACCESS_RDWR;
162 args.start = 0;
163 args.limit = device->info.ram_user - 1;
164 }
165 } else {
166 if (chan->drm->agp.bridge) {
167 args.target = NV_DMA_V0_TARGET_AGP;
168 args.access = NV_DMA_V0_ACCESS_RDWR;
169 args.start = chan->drm->agp.base;
170 args.limit = chan->drm->agp.base +
171 chan->drm->agp.size - 1;
172 } else {
173 args.target = NV_DMA_V0_TARGET_VM;
174 args.access = NV_DMA_V0_ACCESS_RDWR;
175 args.start = 0;
176 args.limit = mmu->limit - 1;
177 }
178 }
179
180 ret = nvif_object_init(&device->object, 0, NV_DMA_FROM_MEMORY,
181 &args, sizeof(args), &chan->push.ctxdma);
182 if (ret) {
183 nouveau_channel_del(pchan);
184 return ret;
185 }
186
187 return 0;
188 }
189
190 static int
191 nouveau_channel_ind(struct nouveau_drm *drm, struct nvif_device *device,
192 u32 engine, struct nouveau_channel **pchan)
193 {
194 static const u16 oclasses[] = { MAXWELL_CHANNEL_GPFIFO_A,
195 KEPLER_CHANNEL_GPFIFO_A,
196 FERMI_CHANNEL_GPFIFO,
197 G82_CHANNEL_GPFIFO,
198 NV50_CHANNEL_GPFIFO,
199 0 };
200 const u16 *oclass = oclasses;
201 union {
202 struct nv50_channel_gpfifo_v0 nv50;
203 struct fermi_channel_gpfifo_v0 fermi;
204 struct kepler_channel_gpfifo_a_v0 kepler;
205 } args;
206 struct nouveau_channel *chan;
207 u32 size;
208 int ret;
209
210 /* allocate dma push buffer */
211 ret = nouveau_channel_prep(drm, device, 0x12000, &chan);
212 *pchan = chan;
213 if (ret)
214 return ret;
215
216 /* create channel object */
217 do {
218 if (oclass[0] >= KEPLER_CHANNEL_GPFIFO_A) {
219 args.kepler.version = 0;
220 args.kepler.engine = engine;
221 args.kepler.ilength = 0x02000;
222 args.kepler.ioffset = 0x10000 + chan->push.vma.offset;
223 args.kepler.vm = 0;
224 size = sizeof(args.kepler);
225 } else
226 if (oclass[0] >= FERMI_CHANNEL_GPFIFO) {
227 args.fermi.version = 0;
228 args.fermi.ilength = 0x02000;
229 args.fermi.ioffset = 0x10000 + chan->push.vma.offset;
230 args.fermi.vm = 0;
231 size = sizeof(args.fermi);
232 } else {
233 args.nv50.version = 0;
234 args.nv50.ilength = 0x02000;
235 args.nv50.ioffset = 0x10000 + chan->push.vma.offset;
236 args.nv50.pushbuf = nvif_handle(&chan->push.ctxdma);
237 args.nv50.vm = 0;
238 size = sizeof(args.nv50);
239 }
240
241 ret = nvif_object_init(&device->object, 0, *oclass++,
242 &args, size, &chan->user);
243 if (ret == 0) {
244 if (chan->user.oclass >= KEPLER_CHANNEL_GPFIFO_A)
245 chan->chid = args.kepler.chid;
246 else
247 if (chan->user.oclass >= FERMI_CHANNEL_GPFIFO)
248 chan->chid = args.fermi.chid;
249 else
250 chan->chid = args.nv50.chid;
251 return ret;
252 }
253 } while (*oclass);
254
255 nouveau_channel_del(pchan);
256 return ret;
257 }
258
259 static int
260 nouveau_channel_dma(struct nouveau_drm *drm, struct nvif_device *device,
261 struct nouveau_channel **pchan)
262 {
263 static const u16 oclasses[] = { NV40_CHANNEL_DMA,
264 NV17_CHANNEL_DMA,
265 NV10_CHANNEL_DMA,
266 NV03_CHANNEL_DMA,
267 0 };
268 const u16 *oclass = oclasses;
269 struct nv03_channel_dma_v0 args;
270 struct nouveau_channel *chan;
271 int ret;
272
273 /* allocate dma push buffer */
274 ret = nouveau_channel_prep(drm, device, 0x10000, &chan);
275 *pchan = chan;
276 if (ret)
277 return ret;
278
279 /* create channel object */
280 args.version = 0;
281 args.pushbuf = nvif_handle(&chan->push.ctxdma);
282 args.offset = chan->push.vma.offset;
283
284 do {
285 ret = nvif_object_init(&device->object, 0, *oclass++,
286 &args, sizeof(args), &chan->user);
287 if (ret == 0) {
288 chan->chid = args.chid;
289 return ret;
290 }
291 } while (ret && *oclass);
292
293 nouveau_channel_del(pchan);
294 return ret;
295 }
296
297 static int
298 nouveau_channel_init(struct nouveau_channel *chan, u32 vram, u32 gart)
299 {
300 struct nvif_device *device = chan->device;
301 struct nouveau_cli *cli = (void *)chan->user.client;
302 struct nvkm_mmu *mmu = nvxx_mmu(device);
303 struct nv_dma_v0 args = {};
304 int ret, i;
305
306 ret = nvif_object_map(&chan->user);
307 if (ret) {
308 NV_PRINTK(err, cli, "nvif_object_map, %d\n", ret);
309 return ret;
310 }
311
312 /* allocate dma objects to cover all allowed vram, and gart */
313 if (device->info.family < NV_DEVICE_INFO_V0_FERMI) {
314 if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
315 args.target = NV_DMA_V0_TARGET_VM;
316 args.access = NV_DMA_V0_ACCESS_VM;
317 args.start = 0;
318 args.limit = cli->vm->mmu->limit - 1;
319 } else {
320 args.target = NV_DMA_V0_TARGET_VRAM;
321 args.access = NV_DMA_V0_ACCESS_RDWR;
322 args.start = 0;
323 args.limit = device->info.ram_user - 1;
324 }
325
326 ret = nvif_object_init(&chan->user, vram, NV_DMA_IN_MEMORY,
327 &args, sizeof(args), &chan->vram);
328 if (ret)
329 return ret;
330
331 if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
332 args.target = NV_DMA_V0_TARGET_VM;
333 args.access = NV_DMA_V0_ACCESS_VM;
334 args.start = 0;
335 args.limit = cli->vm->mmu->limit - 1;
336 } else
337 if (chan->drm->agp.bridge) {
338 args.target = NV_DMA_V0_TARGET_AGP;
339 args.access = NV_DMA_V0_ACCESS_RDWR;
340 args.start = chan->drm->agp.base;
341 args.limit = chan->drm->agp.base +
342 chan->drm->agp.size - 1;
343 } else {
344 args.target = NV_DMA_V0_TARGET_VM;
345 args.access = NV_DMA_V0_ACCESS_RDWR;
346 args.start = 0;
347 args.limit = mmu->limit - 1;
348 }
349
350 ret = nvif_object_init(&chan->user, gart, NV_DMA_IN_MEMORY,
351 &args, sizeof(args), &chan->gart);
352 if (ret)
353 return ret;
354 }
355
356 /* initialise dma tracking parameters */
357 switch (chan->user.oclass & 0x00ff) {
358 case 0x006b:
359 case 0x006e:
360 chan->user_put = 0x40;
361 chan->user_get = 0x44;
362 chan->dma.max = (0x10000 / 4) - 2;
363 break;
364 default:
365 chan->user_put = 0x40;
366 chan->user_get = 0x44;
367 chan->user_get_hi = 0x60;
368 chan->dma.ib_base = 0x10000 / 4;
369 chan->dma.ib_max = (0x02000 / 8) - 1;
370 chan->dma.ib_put = 0;
371 chan->dma.ib_free = chan->dma.ib_max - chan->dma.ib_put;
372 chan->dma.max = chan->dma.ib_base;
373 break;
374 }
375
376 chan->dma.put = 0;
377 chan->dma.cur = chan->dma.put;
378 chan->dma.free = chan->dma.max - chan->dma.cur;
379
380 ret = RING_SPACE(chan, NOUVEAU_DMA_SKIPS);
381 if (ret)
382 return ret;
383
384 for (i = 0; i < NOUVEAU_DMA_SKIPS; i++)
385 OUT_RING(chan, 0x00000000);
386
387 /* allocate software object class (used for fences on <= nv05) */
388 if (device->info.family < NV_DEVICE_INFO_V0_CELSIUS) {
389 ret = nvif_object_init(&chan->user, 0x006e,
390 NVIF_IOCTL_NEW_V0_SW_NV04,
391 NULL, 0, &chan->nvsw);
392 if (ret)
393 return ret;
394
395 ret = RING_SPACE(chan, 2);
396 if (ret)
397 return ret;
398
399 BEGIN_NV04(chan, NvSubSw, 0x0000, 1);
400 OUT_RING (chan, chan->nvsw.handle);
401 FIRE_RING (chan);
402 }
403
404 /* initialise synchronisation */
405 return nouveau_fence(chan->drm)->context_new(chan);
406 }
407
408 int
409 nouveau_channel_new(struct nouveau_drm *drm, struct nvif_device *device,
410 u32 arg0, u32 arg1, struct nouveau_channel **pchan)
411 {
412 struct nouveau_cli *cli = (void *)device->object.client;
413 bool super;
414 int ret;
415
416 /* hack until fencenv50 is fixed, and agp access relaxed */
417 super = cli->base.super;
418 cli->base.super = true;
419
420 ret = nouveau_channel_ind(drm, device, arg0, pchan);
421 if (ret) {
422 NV_PRINTK(dbg, cli, "ib channel create, %d\n", ret);
423 ret = nouveau_channel_dma(drm, device, pchan);
424 if (ret) {
425 NV_PRINTK(dbg, cli, "dma channel create, %d\n", ret);
426 goto done;
427 }
428 }
429
430 ret = nouveau_channel_init(*pchan, arg0, arg1);
431 if (ret) {
432 NV_PRINTK(err, cli, "channel failed to initialise, %d\n", ret);
433 nouveau_channel_del(pchan);
434 }
435
436 done:
437 cli->base.super = super;
438 return ret;
439 }
440