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.
25 #include <core/object.h>
26 #include <core/client.h>
27 #include <core/device.h>
28 #include <core/class.h>
30 #include <subdev/fb.h>
31 #include <subdev/vm.h>
32 #include <subdev/instmem.h>
34 #include <engine/software.h>
36 #include "nouveau_drm.h"
37 #include "nouveau_dma.h"
38 #include "nouveau_bo.h"
39 #include "nouveau_chan.h"
40 #include "nouveau_fence.h"
41 #include "nouveau_abi16.h"
43 MODULE_PARM_DESC(vram_pushbuf, "Create DMA push buffers in VRAM");
44 static int nouveau_vram_pushbuf;
45 module_param_named(vram_pushbuf, nouveau_vram_pushbuf, int, 0400);
48 nouveau_channel_idle(struct nouveau_channel *chan)
50 struct nouveau_cli *cli = chan->cli;
51 struct nouveau_fence *fence = NULL;
54 ret = nouveau_fence_new(chan, false, &fence);
56 ret = nouveau_fence_wait(fence, false, false);
57 nouveau_fence_unref(&fence);
61 NV_ERROR(cli, "failed to idle channel 0x%08x [%s]\n",
62 chan->handle, cli->base.name);
67 nouveau_channel_del(struct nouveau_channel **pchan)
69 struct nouveau_channel *chan = *pchan;
71 struct nouveau_object *client = nv_object(chan->cli);
73 nouveau_channel_idle(chan);
74 nouveau_fence(chan->drm)->context_del(chan);
76 nouveau_object_del(client, NVDRM_DEVICE, chan->handle);
77 nouveau_object_del(client, NVDRM_DEVICE, chan->push.handle);
78 nouveau_bo_vma_del(chan->push.buffer, &chan->push.vma);
79 nouveau_bo_unmap(chan->push.buffer);
80 if (chan->push.buffer && chan->push.buffer->pin_refcnt)
81 nouveau_bo_unpin(chan->push.buffer);
82 nouveau_bo_ref(NULL, &chan->push.buffer);
89 nouveau_channel_prep(struct nouveau_drm *drm, struct nouveau_cli *cli,
90 u32 parent, u32 handle, u32 size,
91 struct nouveau_channel **pchan)
93 struct nouveau_device *device = nv_device(drm->device);
94 struct nouveau_instmem *imem = nouveau_instmem(device);
95 struct nouveau_vmmgr *vmm = nouveau_vmmgr(device);
96 struct nouveau_fb *pfb = nouveau_fb(device);
97 struct nouveau_client *client = &cli->base;
98 struct nv_dma_class args = {};
99 struct nouveau_channel *chan;
100 struct nouveau_object *push;
104 chan = *pchan = kzalloc(sizeof(*chan), GFP_KERNEL);
110 chan->handle = handle;
112 /* allocate memory for dma push buffer */
113 target = TTM_PL_FLAG_TT;
114 if (nouveau_vram_pushbuf)
115 target = TTM_PL_FLAG_VRAM;
117 ret = nouveau_bo_new(drm->dev, size, 0, target, 0, 0, NULL,
120 ret = nouveau_bo_pin(chan->push.buffer, target);
122 ret = nouveau_bo_map(chan->push.buffer);
126 nouveau_channel_del(pchan);
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
134 chan->push.vma.offset = chan->push.buffer->bo.offset;
135 chan->push.handle = NVDRM_PUSH | (handle & 0xffff);
137 if (device->card_type >= NV_50) {
138 ret = nouveau_bo_vma_add(chan->push.buffer, client->vm,
141 nouveau_channel_del(pchan);
145 args.flags = NV_DMA_TARGET_VM | NV_DMA_ACCESS_VM;
147 args.limit = client->vm->vmm->limit - 1;
149 if (chan->push.buffer->bo.mem.mem_type == TTM_PL_VRAM) {
150 u64 limit = pfb->ram->size - imem->reserved - 1;
151 if (device->card_type == NV_04) {
152 /* nv04 vram pushbuf hack, retarget to its location in
153 * the framebuffer bar rather than direct vram access..
154 * nfi why this exists, it came from the -nv ddx.
156 args.flags = NV_DMA_TARGET_PCI | NV_DMA_ACCESS_RDWR;
157 args.start = pci_resource_start(device->pdev, 1);
158 args.limit = args.start + limit;
160 args.flags = NV_DMA_TARGET_VRAM | NV_DMA_ACCESS_RDWR;
165 if (chan->drm->agp.stat == ENABLED) {
166 args.flags = NV_DMA_TARGET_AGP | NV_DMA_ACCESS_RDWR;
167 args.start = chan->drm->agp.base;
168 args.limit = chan->drm->agp.base +
169 chan->drm->agp.size - 1;
171 args.flags = NV_DMA_TARGET_VM | NV_DMA_ACCESS_RDWR;
173 args.limit = vmm->limit - 1;
177 ret = nouveau_object_new(nv_object(chan->cli), parent,
178 chan->push.handle, 0x0002,
179 &args, sizeof(args), &push);
181 nouveau_channel_del(pchan);
189 nouveau_channel_ind(struct nouveau_drm *drm, struct nouveau_cli *cli,
190 u32 parent, u32 handle, u32 engine,
191 struct nouveau_channel **pchan)
193 static const u16 oclasses[] = { NVE0_CHANNEL_IND_CLASS,
194 NVC0_CHANNEL_IND_CLASS,
195 NV84_CHANNEL_IND_CLASS,
196 NV50_CHANNEL_IND_CLASS,
198 const u16 *oclass = oclasses;
199 struct nve0_channel_ind_class args;
200 struct nouveau_channel *chan;
203 /* allocate dma push buffer */
204 ret = nouveau_channel_prep(drm, cli, parent, handle, 0x12000, &chan);
209 /* create channel object */
210 args.pushbuf = chan->push.handle;
211 args.ioffset = 0x10000 + chan->push.vma.offset;
212 args.ilength = 0x02000;
213 args.engine = engine;
216 ret = nouveau_object_new(nv_object(cli), parent, handle,
217 *oclass++, &args, sizeof(args),
223 nouveau_channel_del(pchan);
228 nouveau_channel_dma(struct nouveau_drm *drm, struct nouveau_cli *cli,
229 u32 parent, u32 handle, struct nouveau_channel **pchan)
231 static const u16 oclasses[] = { NV40_CHANNEL_DMA_CLASS,
232 NV17_CHANNEL_DMA_CLASS,
233 NV10_CHANNEL_DMA_CLASS,
234 NV03_CHANNEL_DMA_CLASS,
236 const u16 *oclass = oclasses;
237 struct nv03_channel_dma_class args;
238 struct nouveau_channel *chan;
241 /* allocate dma push buffer */
242 ret = nouveau_channel_prep(drm, cli, parent, handle, 0x10000, &chan);
247 /* create channel object */
248 args.pushbuf = chan->push.handle;
249 args.offset = chan->push.vma.offset;
252 ret = nouveau_object_new(nv_object(cli), parent, handle,
253 *oclass++, &args, sizeof(args),
257 } while (ret && *oclass);
259 nouveau_channel_del(pchan);
264 nouveau_channel_init(struct nouveau_channel *chan, u32 vram, u32 gart)
266 struct nouveau_client *client = nv_client(chan->cli);
267 struct nouveau_device *device = nv_device(chan->drm->device);
268 struct nouveau_instmem *imem = nouveau_instmem(device);
269 struct nouveau_vmmgr *vmm = nouveau_vmmgr(device);
270 struct nouveau_fb *pfb = nouveau_fb(device);
271 struct nouveau_software_chan *swch;
272 struct nouveau_object *object;
273 struct nv_dma_class args = {};
276 /* allocate dma objects to cover all allowed vram, and gart */
277 if (device->card_type < NV_C0) {
278 if (device->card_type >= NV_50) {
279 args.flags = NV_DMA_TARGET_VM | NV_DMA_ACCESS_VM;
281 args.limit = client->vm->vmm->limit - 1;
283 args.flags = NV_DMA_TARGET_VRAM | NV_DMA_ACCESS_RDWR;
285 args.limit = pfb->ram->size - imem->reserved - 1;
288 ret = nouveau_object_new(nv_object(client), chan->handle, vram,
289 0x003d, &args, sizeof(args), &object);
293 if (device->card_type >= NV_50) {
294 args.flags = NV_DMA_TARGET_VM | NV_DMA_ACCESS_VM;
296 args.limit = client->vm->vmm->limit - 1;
298 if (chan->drm->agp.stat == ENABLED) {
299 args.flags = NV_DMA_TARGET_AGP | NV_DMA_ACCESS_RDWR;
300 args.start = chan->drm->agp.base;
301 args.limit = chan->drm->agp.base +
302 chan->drm->agp.size - 1;
304 args.flags = NV_DMA_TARGET_VM | NV_DMA_ACCESS_RDWR;
306 args.limit = vmm->limit - 1;
309 ret = nouveau_object_new(nv_object(client), chan->handle, gart,
310 0x003d, &args, sizeof(args), &object);
318 /* initialise dma tracking parameters */
319 switch (nv_hclass(chan->object) & 0x00ff) {
322 chan->user_put = 0x40;
323 chan->user_get = 0x44;
324 chan->dma.max = (0x10000 / 4) - 2;
327 chan->user_put = 0x40;
328 chan->user_get = 0x44;
329 chan->user_get_hi = 0x60;
330 chan->dma.ib_base = 0x10000 / 4;
331 chan->dma.ib_max = (0x02000 / 8) - 1;
332 chan->dma.ib_put = 0;
333 chan->dma.ib_free = chan->dma.ib_max - chan->dma.ib_put;
334 chan->dma.max = chan->dma.ib_base;
339 chan->dma.cur = chan->dma.put;
340 chan->dma.free = chan->dma.max - chan->dma.cur;
342 ret = RING_SPACE(chan, NOUVEAU_DMA_SKIPS);
346 for (i = 0; i < NOUVEAU_DMA_SKIPS; i++)
347 OUT_RING(chan, 0x00000000);
349 /* allocate software object class (used for fences on <= nv05) */
350 if (device->card_type < NV_10) {
351 ret = nouveau_object_new(nv_object(client), chan->handle,
352 NvSw, 0x006e, NULL, 0, &object);
356 swch = (void *)object->parent;
357 swch->flip = nouveau_flip_complete;
358 swch->flip_data = chan;
360 ret = RING_SPACE(chan, 2);
364 BEGIN_NV04(chan, NvSubSw, 0x0000, 1);
365 OUT_RING (chan, NvSw);
369 /* initialise synchronisation */
370 return nouveau_fence(chan->drm)->context_new(chan);
374 nouveau_channel_new(struct nouveau_drm *drm, struct nouveau_cli *cli,
375 u32 parent, u32 handle, u32 arg0, u32 arg1,
376 struct nouveau_channel **pchan)
380 ret = nouveau_channel_ind(drm, cli, parent, handle, arg0, pchan);
382 NV_DEBUG(cli, "ib channel create, %d\n", ret);
383 ret = nouveau_channel_dma(drm, cli, parent, handle, pchan);
385 NV_DEBUG(cli, "dma channel create, %d\n", ret);
390 ret = nouveau_channel_init(*pchan, arg0, arg1);
392 NV_ERROR(cli, "channel failed to initialise, %d\n", ret);
393 nouveau_channel_del(pchan);