blob: b63a56e07435189f6e945c1fb8f6a156ed2886db [file] [log] [blame]
Ben Skeggsebb945a2012-07-20 08:17:34 +10001/*
2 * Copyright 2012 Red Hat Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * Authors: Ben Skeggs
23 */
24
Ben Skeggsfdb751e2014-08-10 04:10:23 +100025#include <nvif/os.h>
26#include <nvif/class.h>
27
28/*XXX*/
Ben Skeggsebb945a2012-07-20 08:17:34 +100029#include <core/client.h>
Ben Skeggsebb945a2012-07-20 08:17:34 +100030
Ben Skeggsebb945a2012-07-20 08:17:34 +100031#include "nouveau_drm.h"
32#include "nouveau_dma.h"
33#include "nouveau_bo.h"
34#include "nouveau_chan.h"
35#include "nouveau_fence.h"
36#include "nouveau_abi16.h"
37
38MODULE_PARM_DESC(vram_pushbuf, "Create DMA push buffers in VRAM");
39static int nouveau_vram_pushbuf;
40module_param_named(vram_pushbuf, nouveau_vram_pushbuf, int, 0400);
41
42int
43nouveau_channel_idle(struct nouveau_channel *chan)
44{
Ben Skeggs0ad72862014-08-10 04:10:22 +100045 struct nouveau_cli *cli = (void *)nvif_client(chan->object);
Ben Skeggsebb945a2012-07-20 08:17:34 +100046 struct nouveau_fence *fence = NULL;
47 int ret;
48
Ben Skeggs264ce192013-02-14 13:43:21 +100049 ret = nouveau_fence_new(chan, false, &fence);
Ben Skeggsebb945a2012-07-20 08:17:34 +100050 if (!ret) {
51 ret = nouveau_fence_wait(fence, false, false);
52 nouveau_fence_unref(&fence);
53 }
54
55 if (ret)
Ben Skeggsfa2bade2014-08-10 04:10:22 +100056 NV_PRINTK(error, cli, "failed to idle channel 0x%08x [%s]\n",
Ben Skeggs0ad72862014-08-10 04:10:22 +100057 chan->object->handle, nvkm_client(&cli->base)->name);
Ben Skeggsebb945a2012-07-20 08:17:34 +100058 return ret;
59}
60
61void
62nouveau_channel_del(struct nouveau_channel **pchan)
63{
64 struct nouveau_channel *chan = *pchan;
65 if (chan) {
Ben Skeggsebb945a2012-07-20 08:17:34 +100066 if (chan->fence) {
67 nouveau_channel_idle(chan);
68 nouveau_fence(chan->drm)->context_del(chan);
69 }
Ben Skeggs0ad72862014-08-10 04:10:22 +100070 nvif_object_fini(&chan->nvsw);
71 nvif_object_fini(&chan->gart);
72 nvif_object_fini(&chan->vram);
73 nvif_object_ref(NULL, &chan->object);
74 nvif_object_fini(&chan->push.ctxdma);
Ben Skeggsebb945a2012-07-20 08:17:34 +100075 nouveau_bo_vma_del(chan->push.buffer, &chan->push.vma);
76 nouveau_bo_unmap(chan->push.buffer);
Marcin Slusarz124ea292012-11-25 23:02:28 +010077 if (chan->push.buffer && chan->push.buffer->pin_refcnt)
78 nouveau_bo_unpin(chan->push.buffer);
Ben Skeggsebb945a2012-07-20 08:17:34 +100079 nouveau_bo_ref(NULL, &chan->push.buffer);
Ben Skeggs0ad72862014-08-10 04:10:22 +100080 nvif_device_ref(NULL, &chan->device);
Ben Skeggsebb945a2012-07-20 08:17:34 +100081 kfree(chan);
82 }
83 *pchan = NULL;
84}
85
86static int
Ben Skeggs0ad72862014-08-10 04:10:22 +100087nouveau_channel_prep(struct nouveau_drm *drm, struct nvif_device *device,
88 u32 handle, u32 size, struct nouveau_channel **pchan)
Ben Skeggsebb945a2012-07-20 08:17:34 +100089{
Ben Skeggs0ad72862014-08-10 04:10:22 +100090 struct nouveau_cli *cli = (void *)nvif_client(&device->base);
Ben Skeggs967e7bd2014-08-10 04:10:22 +100091 struct nouveau_instmem *imem = nvkm_instmem(device);
92 struct nouveau_vmmgr *vmm = nvkm_vmmgr(device);
93 struct nouveau_fb *pfb = nvkm_fb(device);
Ben Skeggsebb945a2012-07-20 08:17:34 +100094 struct nv_dma_class args = {};
95 struct nouveau_channel *chan;
Ben Skeggsebb945a2012-07-20 08:17:34 +100096 u32 target;
97 int ret;
98
99 chan = *pchan = kzalloc(sizeof(*chan), GFP_KERNEL);
100 if (!chan)
101 return -ENOMEM;
102
Ben Skeggs0ad72862014-08-10 04:10:22 +1000103 nvif_device_ref(device, &chan->device);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000104 chan->drm = drm;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000105
106 /* allocate memory for dma push buffer */
107 target = TTM_PL_FLAG_TT;
108 if (nouveau_vram_pushbuf)
109 target = TTM_PL_FLAG_VRAM;
110
111 ret = nouveau_bo_new(drm->dev, size, 0, target, 0, 0, NULL,
112 &chan->push.buffer);
113 if (ret == 0) {
114 ret = nouveau_bo_pin(chan->push.buffer, target);
115 if (ret == 0)
116 ret = nouveau_bo_map(chan->push.buffer);
117 }
118
119 if (ret) {
120 nouveau_channel_del(pchan);
121 return ret;
122 }
123
124 /* create dma object covering the *entire* memory space that the
125 * pushbuf lives in, this is because the GEM code requires that
126 * we be able to call out to other (indirect) push buffers
127 */
128 chan->push.vma.offset = chan->push.buffer->bo.offset;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000129
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000130 if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
Ben Skeggs0ad72862014-08-10 04:10:22 +1000131 ret = nouveau_bo_vma_add(chan->push.buffer, cli->vm,
Ben Skeggsebb945a2012-07-20 08:17:34 +1000132 &chan->push.vma);
133 if (ret) {
134 nouveau_channel_del(pchan);
135 return ret;
136 }
137
138 args.flags = NV_DMA_TARGET_VM | NV_DMA_ACCESS_VM;
139 args.start = 0;
Ben Skeggs0ad72862014-08-10 04:10:22 +1000140 args.limit = cli->vm->vmm->limit - 1;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000141 } else
142 if (chan->push.buffer->bo.mem.mem_type == TTM_PL_VRAM) {
Ben Skeggsdceef5d2013-03-04 13:01:21 +1000143 u64 limit = pfb->ram->size - imem->reserved - 1;
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000144 if (device->info.family == NV_DEVICE_INFO_V0_TNT) {
Ben Skeggsebb945a2012-07-20 08:17:34 +1000145 /* nv04 vram pushbuf hack, retarget to its location in
146 * the framebuffer bar rather than direct vram access..
147 * nfi why this exists, it came from the -nv ddx.
148 */
149 args.flags = NV_DMA_TARGET_PCI | NV_DMA_ACCESS_RDWR;
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000150 args.start = nv_device_resource_start(nvkm_device(device), 1);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000151 args.limit = args.start + limit;
152 } else {
153 args.flags = NV_DMA_TARGET_VRAM | NV_DMA_ACCESS_RDWR;
154 args.start = 0;
155 args.limit = limit;
156 }
157 } else {
158 if (chan->drm->agp.stat == ENABLED) {
159 args.flags = NV_DMA_TARGET_AGP | NV_DMA_ACCESS_RDWR;
160 args.start = chan->drm->agp.base;
161 args.limit = chan->drm->agp.base +
162 chan->drm->agp.size - 1;
163 } else {
164 args.flags = NV_DMA_TARGET_VM | NV_DMA_ACCESS_RDWR;
165 args.start = 0;
166 args.limit = vmm->limit - 1;
167 }
168 }
169
Ben Skeggs0ad72862014-08-10 04:10:22 +1000170 ret = nvif_object_init(nvif_object(device), NULL, NVDRM_PUSH |
171 (handle & 0xffff), NV_DMA_FROM_MEMORY_CLASS,
172 &args, sizeof(args), &chan->push.ctxdma);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000173 if (ret) {
174 nouveau_channel_del(pchan);
175 return ret;
176 }
177
178 return 0;
179}
180
Marcin Slusarz5b8a43a2012-08-19 23:00:00 +0200181static int
Ben Skeggs0ad72862014-08-10 04:10:22 +1000182nouveau_channel_ind(struct nouveau_drm *drm, struct nvif_device *device,
183 u32 handle, u32 engine, struct nouveau_channel **pchan)
Ben Skeggsebb945a2012-07-20 08:17:34 +1000184{
Ben Skeggsc97f8c92012-08-19 16:03:00 +1000185 static const u16 oclasses[] = { NVE0_CHANNEL_IND_CLASS,
186 NVC0_CHANNEL_IND_CLASS,
187 NV84_CHANNEL_IND_CLASS,
188 NV50_CHANNEL_IND_CLASS,
189 0 };
Ben Skeggsebb945a2012-07-20 08:17:34 +1000190 const u16 *oclass = oclasses;
Ben Skeggsdbff2de2012-08-06 18:16:37 +1000191 struct nve0_channel_ind_class args;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000192 struct nouveau_channel *chan;
193 int ret;
194
195 /* allocate dma push buffer */
Ben Skeggs0ad72862014-08-10 04:10:22 +1000196 ret = nouveau_channel_prep(drm, device, handle, 0x12000, &chan);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000197 *pchan = chan;
198 if (ret)
199 return ret;
200
201 /* create channel object */
Ben Skeggs0ad72862014-08-10 04:10:22 +1000202 args.pushbuf = chan->push.ctxdma.handle;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000203 args.ioffset = 0x10000 + chan->push.vma.offset;
204 args.ilength = 0x02000;
Ben Skeggs49981042012-08-06 19:38:25 +1000205 args.engine = engine;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000206
207 do {
Ben Skeggs0ad72862014-08-10 04:10:22 +1000208 ret = nvif_object_new(nvif_object(device), handle, *oclass++,
209 &args, sizeof(args), &chan->object);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000210 if (ret == 0)
211 return ret;
212 } while (*oclass);
213
214 nouveau_channel_del(pchan);
215 return ret;
216}
217
218static int
Ben Skeggs0ad72862014-08-10 04:10:22 +1000219nouveau_channel_dma(struct nouveau_drm *drm, struct nvif_device *device,
220 u32 handle, struct nouveau_channel **pchan)
Ben Skeggsebb945a2012-07-20 08:17:34 +1000221{
Ben Skeggsc97f8c92012-08-19 16:03:00 +1000222 static const u16 oclasses[] = { NV40_CHANNEL_DMA_CLASS,
223 NV17_CHANNEL_DMA_CLASS,
224 NV10_CHANNEL_DMA_CLASS,
225 NV03_CHANNEL_DMA_CLASS,
226 0 };
Ben Skeggsebb945a2012-07-20 08:17:34 +1000227 const u16 *oclass = oclasses;
Ben Skeggsa7c6e752012-08-14 15:02:29 +1000228 struct nv03_channel_dma_class args;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000229 struct nouveau_channel *chan;
230 int ret;
231
232 /* allocate dma push buffer */
Ben Skeggs0ad72862014-08-10 04:10:22 +1000233 ret = nouveau_channel_prep(drm, device, handle, 0x10000, &chan);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000234 *pchan = chan;
235 if (ret)
236 return ret;
237
238 /* create channel object */
Ben Skeggs0ad72862014-08-10 04:10:22 +1000239 args.pushbuf = chan->push.ctxdma.handle;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000240 args.offset = chan->push.vma.offset;
241
242 do {
Ben Skeggs0ad72862014-08-10 04:10:22 +1000243 ret = nvif_object_new(nvif_object(device), handle, *oclass++,
244 &args, sizeof(args), &chan->object);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000245 if (ret == 0)
246 return ret;
247 } while (ret && *oclass);
248
249 nouveau_channel_del(pchan);
250 return ret;
251}
252
253static int
254nouveau_channel_init(struct nouveau_channel *chan, u32 vram, u32 gart)
255{
Ben Skeggs0ad72862014-08-10 04:10:22 +1000256 struct nvif_device *device = chan->device;
257 struct nouveau_cli *cli = (void *)nvif_client(&device->base);
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000258 struct nouveau_instmem *imem = nvkm_instmem(device);
259 struct nouveau_vmmgr *vmm = nvkm_vmmgr(device);
260 struct nouveau_fb *pfb = nvkm_fb(device);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000261 struct nouveau_software_chan *swch;
Ben Skeggsf7569442012-10-08 14:29:16 +1000262 struct nv_dma_class args = {};
Ben Skeggsebb945a2012-07-20 08:17:34 +1000263 int ret, i;
264
Ben Skeggsebb945a2012-07-20 08:17:34 +1000265 /* allocate dma objects to cover all allowed vram, and gart */
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000266 if (device->info.family < NV_DEVICE_INFO_V0_FERMI) {
267 if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
Ben Skeggsebb945a2012-07-20 08:17:34 +1000268 args.flags = NV_DMA_TARGET_VM | NV_DMA_ACCESS_VM;
269 args.start = 0;
Ben Skeggs0ad72862014-08-10 04:10:22 +1000270 args.limit = cli->vm->vmm->limit - 1;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000271 } else {
272 args.flags = NV_DMA_TARGET_VRAM | NV_DMA_ACCESS_RDWR;
273 args.start = 0;
Ben Skeggsdceef5d2013-03-04 13:01:21 +1000274 args.limit = pfb->ram->size - imem->reserved - 1;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000275 }
276
Ben Skeggs0ad72862014-08-10 04:10:22 +1000277 ret = nvif_object_init(chan->object, NULL, vram,
278 NV_DMA_IN_MEMORY_CLASS, &args,
279 sizeof(args), &chan->vram);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000280 if (ret)
281 return ret;
282
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000283 if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
Ben Skeggsebb945a2012-07-20 08:17:34 +1000284 args.flags = NV_DMA_TARGET_VM | NV_DMA_ACCESS_VM;
285 args.start = 0;
Ben Skeggs0ad72862014-08-10 04:10:22 +1000286 args.limit = cli->vm->vmm->limit - 1;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000287 } else
288 if (chan->drm->agp.stat == ENABLED) {
289 args.flags = NV_DMA_TARGET_AGP | NV_DMA_ACCESS_RDWR;
290 args.start = chan->drm->agp.base;
291 args.limit = chan->drm->agp.base +
292 chan->drm->agp.size - 1;
293 } else {
294 args.flags = NV_DMA_TARGET_VM | NV_DMA_ACCESS_RDWR;
295 args.start = 0;
296 args.limit = vmm->limit - 1;
297 }
298
Ben Skeggs0ad72862014-08-10 04:10:22 +1000299 ret = nvif_object_init(chan->object, NULL, gart,
300 NV_DMA_IN_MEMORY_CLASS, &args,
301 sizeof(args), &chan->gart);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000302 if (ret)
303 return ret;
304 }
305
306 /* initialise dma tracking parameters */
Ben Skeggs0ad72862014-08-10 04:10:22 +1000307 switch (chan->object->oclass & 0x00ff) {
Ben Skeggs503b0f12012-08-14 14:53:51 +1000308 case 0x006b:
Ben Skeggsebb945a2012-07-20 08:17:34 +1000309 case 0x006e:
310 chan->user_put = 0x40;
311 chan->user_get = 0x44;
312 chan->dma.max = (0x10000 / 4) - 2;
313 break;
314 default:
315 chan->user_put = 0x40;
316 chan->user_get = 0x44;
317 chan->user_get_hi = 0x60;
318 chan->dma.ib_base = 0x10000 / 4;
319 chan->dma.ib_max = (0x02000 / 8) - 1;
320 chan->dma.ib_put = 0;
321 chan->dma.ib_free = chan->dma.ib_max - chan->dma.ib_put;
322 chan->dma.max = chan->dma.ib_base;
323 break;
324 }
325
326 chan->dma.put = 0;
327 chan->dma.cur = chan->dma.put;
328 chan->dma.free = chan->dma.max - chan->dma.cur;
329
330 ret = RING_SPACE(chan, NOUVEAU_DMA_SKIPS);
331 if (ret)
332 return ret;
333
334 for (i = 0; i < NOUVEAU_DMA_SKIPS; i++)
335 OUT_RING(chan, 0x00000000);
336
Ben Skeggs69a61462013-11-13 10:58:51 +1000337 /* allocate software object class (used for fences on <= nv05) */
Ben Skeggs967e7bd2014-08-10 04:10:22 +1000338 if (device->info.family < NV_DEVICE_INFO_V0_CELSIUS) {
Ben Skeggsf45f55c2014-08-10 04:10:23 +1000339 ret = nvif_object_init(chan->object, NULL, 0x006e, 0x006e,
Ben Skeggs0ad72862014-08-10 04:10:22 +1000340 NULL, 0, &chan->nvsw);
Ben Skeggs49981042012-08-06 19:38:25 +1000341 if (ret)
342 return ret;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000343
Ben Skeggs0ad72862014-08-10 04:10:22 +1000344 swch = (void *)nvkm_object(&chan->nvsw)->parent;
Ben Skeggs49981042012-08-06 19:38:25 +1000345 swch->flip = nouveau_flip_complete;
346 swch->flip_data = chan;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000347
Ben Skeggsebb945a2012-07-20 08:17:34 +1000348 ret = RING_SPACE(chan, 2);
349 if (ret)
350 return ret;
351
352 BEGIN_NV04(chan, NvSubSw, 0x0000, 1);
Ben Skeggsf45f55c2014-08-10 04:10:23 +1000353 OUT_RING (chan, chan->nvsw.handle);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000354 FIRE_RING (chan);
355 }
356
357 /* initialise synchronisation */
358 return nouveau_fence(chan->drm)->context_new(chan);
359}
360
361int
Ben Skeggs0ad72862014-08-10 04:10:22 +1000362nouveau_channel_new(struct nouveau_drm *drm, struct nvif_device *device,
363 u32 handle, u32 arg0, u32 arg1,
Ben Skeggsebb945a2012-07-20 08:17:34 +1000364 struct nouveau_channel **pchan)
365{
Ben Skeggs0ad72862014-08-10 04:10:22 +1000366 struct nouveau_cli *cli = (void *)nvif_client(&device->base);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000367 int ret;
368
Ben Skeggs0ad72862014-08-10 04:10:22 +1000369 ret = nouveau_channel_ind(drm, device, handle, arg0, pchan);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000370 if (ret) {
Ben Skeggsfa2bade2014-08-10 04:10:22 +1000371 NV_PRINTK(debug, cli, "ib channel create, %d\n", ret);
Ben Skeggs0ad72862014-08-10 04:10:22 +1000372 ret = nouveau_channel_dma(drm, device, handle, pchan);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000373 if (ret) {
Ben Skeggsfa2bade2014-08-10 04:10:22 +1000374 NV_PRINTK(debug, cli, "dma channel create, %d\n", ret);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000375 return ret;
376 }
377 }
378
Ben Skeggs49981042012-08-06 19:38:25 +1000379 ret = nouveau_channel_init(*pchan, arg0, arg1);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000380 if (ret) {
Ben Skeggsfa2bade2014-08-10 04:10:22 +1000381 NV_PRINTK(error, cli, "channel failed to initialise, %d\n", ret);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000382 nouveau_channel_del(pchan);
383 return ret;
384 }
385
386 return 0;
387}