2 * Copyright 2012 Red Hat Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
26 #include <nvif/class.h>
27 #include <nvif/cl0002.h>
28 #include <nvif/cl006b.h>
29 #include <nvif/cl506f.h>
30 #include <nvif/cl906f.h>
31 #include <nvif/cla06f.h>
32 #include <nvif/clc36f.h>
33 #include <nvif/ioctl.h>
36 #include <core/client.h>
38 #include "nouveau_drv.h"
39 #include "nouveau_dma.h"
40 #include "nouveau_bo.h"
41 #include "nouveau_chan.h"
42 #include "nouveau_fence.h"
43 #include "nouveau_abi16.h"
44 #include "nouveau_vmm.h"
45 #include "nouveau_svm.h"
47 MODULE_PARM_DESC(vram_pushbuf, "Create DMA push buffers in VRAM");
48 int nouveau_vram_pushbuf;
49 module_param_named(vram_pushbuf, nouveau_vram_pushbuf, int, 0400);
52 nouveau_channel_killed(struct nvif_notify *ntfy)
54 struct nouveau_channel *chan = container_of(ntfy, typeof(*chan), kill);
55 struct nouveau_cli *cli = (void *)chan->user.client;
56 NV_PRINTK(warn, cli, "channel %d killed!\n", chan->chid);
57 atomic_set(&chan->killed, 1);
58 return NVIF_NOTIFY_DROP;
62 nouveau_channel_idle(struct nouveau_channel *chan)
64 if (likely(chan && chan->fence && !atomic_read(&chan->killed))) {
65 struct nouveau_cli *cli = (void *)chan->user.client;
66 struct nouveau_fence *fence = NULL;
69 ret = nouveau_fence_new(chan, false, &fence);
71 ret = nouveau_fence_wait(fence, false, false);
72 nouveau_fence_unref(&fence);
76 NV_PRINTK(err, cli, "failed to idle channel %d [%s]\n",
77 chan->chid, nvxx_client(&cli->base)->name);
85 nouveau_channel_del(struct nouveau_channel **pchan)
87 struct nouveau_channel *chan = *pchan;
89 struct nouveau_cli *cli = (void *)chan->user.client;
93 super = cli->base.super;
94 cli->base.super = true;
98 nouveau_fence(chan->drm)->context_del(chan);
101 nouveau_svmm_part(chan->vmm->svmm, chan->inst);
103 nvif_object_fini(&chan->nvsw);
104 nvif_object_fini(&chan->gart);
105 nvif_object_fini(&chan->vram);
106 nvif_notify_fini(&chan->kill);
107 nvif_object_fini(&chan->user);
108 nvif_object_fini(&chan->push.ctxdma);
109 nouveau_vma_del(&chan->push.vma);
110 nouveau_bo_unmap(chan->push.buffer);
111 if (chan->push.buffer && chan->push.buffer->pin_refcnt)
112 nouveau_bo_unpin(chan->push.buffer);
113 nouveau_bo_ref(NULL, &chan->push.buffer);
117 cli->base.super = super;
123 nouveau_channel_prep(struct nouveau_drm *drm, struct nvif_device *device,
124 u32 size, struct nouveau_channel **pchan)
126 struct nouveau_cli *cli = (void *)device->object.client;
127 struct nv_dma_v0 args = {};
128 struct nouveau_channel *chan;
132 chan = *pchan = kzalloc(sizeof(*chan), GFP_KERNEL);
136 chan->device = device;
138 chan->vmm = cli->svm.cli ? &cli->svm : &cli->vmm;
139 atomic_set(&chan->killed, 0);
141 /* allocate memory for dma push buffer */
142 target = TTM_PL_FLAG_TT | TTM_PL_FLAG_UNCACHED;
143 if (nouveau_vram_pushbuf)
144 target = TTM_PL_FLAG_VRAM;
146 ret = nouveau_bo_new(cli, size, 0, target, 0, 0, NULL, NULL,
149 ret = nouveau_bo_pin(chan->push.buffer, target, false);
151 ret = nouveau_bo_map(chan->push.buffer);
155 nouveau_channel_del(pchan);
159 /* create dma object covering the *entire* memory space that the
160 * pushbuf lives in, this is because the GEM code requires that
161 * we be able to call out to other (indirect) push buffers
163 chan->push.addr = chan->push.buffer->bo.offset;
165 if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
166 ret = nouveau_vma_new(chan->push.buffer, chan->vmm,
169 nouveau_channel_del(pchan);
173 chan->push.addr = chan->push.vma->addr;
175 if (device->info.family >= NV_DEVICE_INFO_V0_FERMI)
178 args.target = NV_DMA_V0_TARGET_VM;
179 args.access = NV_DMA_V0_ACCESS_VM;
181 args.limit = chan->vmm->vmm.limit - 1;
183 if (chan->push.buffer->bo.mem.mem_type == TTM_PL_VRAM) {
184 if (device->info.family == NV_DEVICE_INFO_V0_TNT) {
185 /* nv04 vram pushbuf hack, retarget to its location in
186 * the framebuffer bar rather than direct vram access..
187 * nfi why this exists, it came from the -nv ddx.
189 args.target = NV_DMA_V0_TARGET_PCI;
190 args.access = NV_DMA_V0_ACCESS_RDWR;
191 args.start = nvxx_device(device)->func->
192 resource_addr(nvxx_device(device), 1);
193 args.limit = args.start + device->info.ram_user - 1;
195 args.target = NV_DMA_V0_TARGET_VRAM;
196 args.access = NV_DMA_V0_ACCESS_RDWR;
198 args.limit = device->info.ram_user - 1;
201 if (chan->drm->agp.bridge) {
202 args.target = NV_DMA_V0_TARGET_AGP;
203 args.access = NV_DMA_V0_ACCESS_RDWR;
204 args.start = chan->drm->agp.base;
205 args.limit = chan->drm->agp.base +
206 chan->drm->agp.size - 1;
208 args.target = NV_DMA_V0_TARGET_VM;
209 args.access = NV_DMA_V0_ACCESS_RDWR;
211 args.limit = chan->vmm->vmm.limit - 1;
215 ret = nvif_object_init(&device->object, 0, NV_DMA_FROM_MEMORY,
216 &args, sizeof(args), &chan->push.ctxdma);
218 nouveau_channel_del(pchan);
226 nouveau_channel_ind(struct nouveau_drm *drm, struct nvif_device *device,
227 u64 runlist, bool priv, struct nouveau_channel **pchan)
229 static const u16 oclasses[] = { TURING_CHANNEL_GPFIFO_A,
230 VOLTA_CHANNEL_GPFIFO_A,
231 PASCAL_CHANNEL_GPFIFO_A,
232 MAXWELL_CHANNEL_GPFIFO_A,
233 KEPLER_CHANNEL_GPFIFO_B,
234 KEPLER_CHANNEL_GPFIFO_A,
235 FERMI_CHANNEL_GPFIFO,
239 const u16 *oclass = oclasses;
241 struct nv50_channel_gpfifo_v0 nv50;
242 struct fermi_channel_gpfifo_v0 fermi;
243 struct kepler_channel_gpfifo_a_v0 kepler;
244 struct volta_channel_gpfifo_a_v0 volta;
246 struct nouveau_channel *chan;
250 /* allocate dma push buffer */
251 ret = nouveau_channel_prep(drm, device, 0x12000, &chan);
256 /* create channel object */
258 if (oclass[0] >= VOLTA_CHANNEL_GPFIFO_A) {
259 args.volta.version = 0;
260 args.volta.ilength = 0x02000;
261 args.volta.ioffset = 0x10000 + chan->push.addr;
262 args.volta.runlist = runlist;
263 args.volta.vmm = nvif_handle(&chan->vmm->vmm.object);
264 args.volta.priv = priv;
265 size = sizeof(args.volta);
267 if (oclass[0] >= KEPLER_CHANNEL_GPFIFO_A) {
268 args.kepler.version = 0;
269 args.kepler.ilength = 0x02000;
270 args.kepler.ioffset = 0x10000 + chan->push.addr;
271 args.kepler.runlist = runlist;
272 args.kepler.vmm = nvif_handle(&chan->vmm->vmm.object);
273 args.kepler.priv = priv;
274 size = sizeof(args.kepler);
276 if (oclass[0] >= FERMI_CHANNEL_GPFIFO) {
277 args.fermi.version = 0;
278 args.fermi.ilength = 0x02000;
279 args.fermi.ioffset = 0x10000 + chan->push.addr;
280 args.fermi.vmm = nvif_handle(&chan->vmm->vmm.object);
281 size = sizeof(args.fermi);
283 args.nv50.version = 0;
284 args.nv50.ilength = 0x02000;
285 args.nv50.ioffset = 0x10000 + chan->push.addr;
286 args.nv50.pushbuf = nvif_handle(&chan->push.ctxdma);
287 args.nv50.vmm = nvif_handle(&chan->vmm->vmm.object);
288 size = sizeof(args.nv50);
291 ret = nvif_object_init(&device->object, 0, *oclass++,
292 &args, size, &chan->user);
294 if (chan->user.oclass >= VOLTA_CHANNEL_GPFIFO_A) {
295 chan->chid = args.volta.chid;
296 chan->inst = args.volta.inst;
297 chan->token = args.volta.token;
299 if (chan->user.oclass >= KEPLER_CHANNEL_GPFIFO_A) {
300 chan->chid = args.kepler.chid;
301 chan->inst = args.kepler.inst;
303 if (chan->user.oclass >= FERMI_CHANNEL_GPFIFO) {
304 chan->chid = args.fermi.chid;
306 chan->chid = args.nv50.chid;
312 nouveau_channel_del(pchan);
317 nouveau_channel_dma(struct nouveau_drm *drm, struct nvif_device *device,
318 struct nouveau_channel **pchan)
320 static const u16 oclasses[] = { NV40_CHANNEL_DMA,
325 const u16 *oclass = oclasses;
326 struct nv03_channel_dma_v0 args;
327 struct nouveau_channel *chan;
330 /* allocate dma push buffer */
331 ret = nouveau_channel_prep(drm, device, 0x10000, &chan);
336 /* create channel object */
338 args.pushbuf = nvif_handle(&chan->push.ctxdma);
339 args.offset = chan->push.addr;
342 ret = nvif_object_init(&device->object, 0, *oclass++,
343 &args, sizeof(args), &chan->user);
345 chan->chid = args.chid;
348 } while (ret && *oclass);
350 nouveau_channel_del(pchan);
355 nouveau_channel_init(struct nouveau_channel *chan, u32 vram, u32 gart)
357 struct nvif_device *device = chan->device;
358 struct nouveau_drm *drm = chan->drm;
359 struct nv_dma_v0 args = {};
362 nvif_object_map(&chan->user, NULL, 0);
364 if (chan->user.oclass >= FERMI_CHANNEL_GPFIFO) {
365 ret = nvif_notify_init(&chan->user, nouveau_channel_killed,
366 true, NV906F_V0_NTFY_KILLED,
367 NULL, 0, 0, &chan->kill);
369 ret = nvif_notify_get(&chan->kill);
371 NV_ERROR(drm, "Failed to request channel kill "
372 "notification: %d\n", ret);
377 /* allocate dma objects to cover all allowed vram, and gart */
378 if (device->info.family < NV_DEVICE_INFO_V0_FERMI) {
379 if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
380 args.target = NV_DMA_V0_TARGET_VM;
381 args.access = NV_DMA_V0_ACCESS_VM;
383 args.limit = chan->vmm->vmm.limit - 1;
385 args.target = NV_DMA_V0_TARGET_VRAM;
386 args.access = NV_DMA_V0_ACCESS_RDWR;
388 args.limit = device->info.ram_user - 1;
391 ret = nvif_object_init(&chan->user, vram, NV_DMA_IN_MEMORY,
392 &args, sizeof(args), &chan->vram);
396 if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
397 args.target = NV_DMA_V0_TARGET_VM;
398 args.access = NV_DMA_V0_ACCESS_VM;
400 args.limit = chan->vmm->vmm.limit - 1;
402 if (chan->drm->agp.bridge) {
403 args.target = NV_DMA_V0_TARGET_AGP;
404 args.access = NV_DMA_V0_ACCESS_RDWR;
405 args.start = chan->drm->agp.base;
406 args.limit = chan->drm->agp.base +
407 chan->drm->agp.size - 1;
409 args.target = NV_DMA_V0_TARGET_VM;
410 args.access = NV_DMA_V0_ACCESS_RDWR;
412 args.limit = chan->vmm->vmm.limit - 1;
415 ret = nvif_object_init(&chan->user, gart, NV_DMA_IN_MEMORY,
416 &args, sizeof(args), &chan->gart);
421 /* initialise dma tracking parameters */
422 switch (chan->user.oclass & 0x00ff) {
425 chan->user_put = 0x40;
426 chan->user_get = 0x44;
427 chan->dma.max = (0x10000 / 4) - 2;
430 chan->user_put = 0x40;
431 chan->user_get = 0x44;
432 chan->user_get_hi = 0x60;
433 chan->dma.ib_base = 0x10000 / 4;
434 chan->dma.ib_max = (0x02000 / 8) - 1;
435 chan->dma.ib_put = 0;
436 chan->dma.ib_free = chan->dma.ib_max - chan->dma.ib_put;
437 chan->dma.max = chan->dma.ib_base;
442 chan->dma.cur = chan->dma.put;
443 chan->dma.free = chan->dma.max - chan->dma.cur;
445 ret = RING_SPACE(chan, NOUVEAU_DMA_SKIPS);
449 for (i = 0; i < NOUVEAU_DMA_SKIPS; i++)
450 OUT_RING(chan, 0x00000000);
452 /* allocate software object class (used for fences on <= nv05) */
453 if (device->info.family < NV_DEVICE_INFO_V0_CELSIUS) {
454 ret = nvif_object_init(&chan->user, 0x006e,
456 NULL, 0, &chan->nvsw);
460 ret = RING_SPACE(chan, 2);
464 BEGIN_NV04(chan, NvSubSw, 0x0000, 1);
465 OUT_RING (chan, chan->nvsw.handle);
469 /* initialise synchronisation */
470 return nouveau_fence(chan->drm)->context_new(chan);
474 nouveau_channel_new(struct nouveau_drm *drm, struct nvif_device *device,
475 u32 arg0, u32 arg1, bool priv,
476 struct nouveau_channel **pchan)
478 struct nouveau_cli *cli = (void *)device->object.client;
482 /* hack until fencenv50 is fixed, and agp access relaxed */
483 super = cli->base.super;
484 cli->base.super = true;
486 ret = nouveau_channel_ind(drm, device, arg0, priv, pchan);
488 NV_PRINTK(dbg, cli, "ib channel create, %d\n", ret);
489 ret = nouveau_channel_dma(drm, device, pchan);
491 NV_PRINTK(dbg, cli, "dma channel create, %d\n", ret);
496 ret = nouveau_channel_init(*pchan, arg0, arg1);
498 NV_PRINTK(err, cli, "channel failed to initialise, %d\n", ret);
499 nouveau_channel_del(pchan);
502 ret = nouveau_svmm_join((*pchan)->vmm->svmm, (*pchan)->inst);
504 nouveau_channel_del(pchan);
507 cli->base.super = super;
512 nouveau_channels_init(struct nouveau_drm *drm)
515 struct nv_device_info_v1 m;
517 struct nv_device_info_v1_data channels;
521 .m.count = sizeof(args.v) / sizeof(args.v.channels),
522 .v.channels.mthd = NV_DEVICE_FIFO_CHANNELS,
524 struct nvif_object *device = &drm->client.device.object;
527 ret = nvif_object_mthd(device, NV_DEVICE_V0_INFO, &args, sizeof(args));
528 if (ret || args.v.channels.mthd == NV_DEVICE_INFO_INVALID)
531 drm->chan.nr = args.v.channels.data;
532 drm->chan.context_base = dma_fence_context_alloc(drm->chan.nr);