xref: /linux/drivers/gpu/drm/nouveau/nouveau_chan.c (revision fdb06e2b2a18b3650526857ccb1ea95333282b90)
1ebb945a9SBen Skeggs /*
2ebb945a9SBen Skeggs  * Copyright 2012 Red Hat Inc.
3ebb945a9SBen Skeggs  *
4ebb945a9SBen Skeggs  * Permission is hereby granted, free of charge, to any person obtaining a
5ebb945a9SBen Skeggs  * copy of this software and associated documentation files (the "Software"),
6ebb945a9SBen Skeggs  * to deal in the Software without restriction, including without limitation
7ebb945a9SBen Skeggs  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8ebb945a9SBen Skeggs  * and/or sell copies of the Software, and to permit persons to whom the
9ebb945a9SBen Skeggs  * Software is furnished to do so, subject to the following conditions:
10ebb945a9SBen Skeggs  *
11ebb945a9SBen Skeggs  * The above copyright notice and this permission notice shall be included in
12ebb945a9SBen Skeggs  * all copies or substantial portions of the Software.
13ebb945a9SBen Skeggs  *
14ebb945a9SBen Skeggs  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15ebb945a9SBen Skeggs  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16ebb945a9SBen Skeggs  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17ebb945a9SBen Skeggs  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18ebb945a9SBen Skeggs  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19ebb945a9SBen Skeggs  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20ebb945a9SBen Skeggs  * OTHER DEALINGS IN THE SOFTWARE.
21ebb945a9SBen Skeggs  *
22ebb945a9SBen Skeggs  * Authors: Ben Skeggs
23ebb945a9SBen Skeggs  */
24ebb945a9SBen Skeggs 
25fdb751efSBen Skeggs #include <nvif/os.h>
26fdb751efSBen Skeggs #include <nvif/class.h>
27845f2725SBen Skeggs #include <nvif/cl0002.h>
288ed1730cSBen Skeggs #include <nvif/cl006b.h>
298ed1730cSBen Skeggs #include <nvif/cl506f.h>
308ed1730cSBen Skeggs #include <nvif/cl906f.h>
318ed1730cSBen Skeggs #include <nvif/cla06f.h>
329d24907cSBen Skeggs #include <nvif/clc36f.h>
33f58ddf95SBen Skeggs #include <nvif/ioctl.h>
34fdb751efSBen Skeggs 
35fdb751efSBen Skeggs /*XXX*/
36ebb945a9SBen Skeggs #include <core/client.h>
37ebb945a9SBen Skeggs 
384dc28134SBen Skeggs #include "nouveau_drv.h"
39ebb945a9SBen Skeggs #include "nouveau_dma.h"
40ebb945a9SBen Skeggs #include "nouveau_bo.h"
41ebb945a9SBen Skeggs #include "nouveau_chan.h"
42ebb945a9SBen Skeggs #include "nouveau_fence.h"
43ebb945a9SBen Skeggs #include "nouveau_abi16.h"
4424e8375bSBen Skeggs #include "nouveau_vmm.h"
45eeaf06acSBen Skeggs #include "nouveau_svm.h"
46ebb945a9SBen Skeggs 
47ebb945a9SBen Skeggs MODULE_PARM_DESC(vram_pushbuf, "Create DMA push buffers in VRAM");
48703fa264SPierre Moreau int nouveau_vram_pushbuf;
49ebb945a9SBen Skeggs module_param_named(vram_pushbuf, nouveau_vram_pushbuf, int, 0400);
50ebb945a9SBen Skeggs 
51d8cc37d8SBen Skeggs static int
52d8cc37d8SBen Skeggs nouveau_channel_killed(struct nvif_notify *ntfy)
53d8cc37d8SBen Skeggs {
54d8cc37d8SBen Skeggs 	struct nouveau_channel *chan = container_of(ntfy, typeof(*chan), kill);
55d8cc37d8SBen Skeggs 	struct nouveau_cli *cli = (void *)chan->user.client;
56d8cc37d8SBen Skeggs 	NV_PRINTK(warn, cli, "channel %d killed!\n", chan->chid);
57d8cc37d8SBen Skeggs 	atomic_set(&chan->killed, 1);
58ea13e5abSBen Skeggs 	if (chan->fence)
59ea13e5abSBen Skeggs 		nouveau_fence_context_kill(chan->fence, -ENODEV);
60d8cc37d8SBen Skeggs 	return NVIF_NOTIFY_DROP;
61d8cc37d8SBen Skeggs }
62d8cc37d8SBen Skeggs 
63ebb945a9SBen Skeggs int
64ebb945a9SBen Skeggs nouveau_channel_idle(struct nouveau_channel *chan)
65ebb945a9SBen Skeggs {
6684cd0a55SBen Skeggs 	if (likely(chan && chan->fence && !atomic_read(&chan->killed))) {
67a01ca78cSBen Skeggs 		struct nouveau_cli *cli = (void *)chan->user.client;
68ebb945a9SBen Skeggs 		struct nouveau_fence *fence = NULL;
69ebb945a9SBen Skeggs 		int ret;
70ebb945a9SBen Skeggs 
71264ce192SBen Skeggs 		ret = nouveau_fence_new(chan, false, &fence);
72ebb945a9SBen Skeggs 		if (!ret) {
73ebb945a9SBen Skeggs 			ret = nouveau_fence_wait(fence, false, false);
74ebb945a9SBen Skeggs 			nouveau_fence_unref(&fence);
75ebb945a9SBen Skeggs 		}
76ebb945a9SBen Skeggs 
77fbd58ebdSBen Skeggs 		if (ret) {
78fcf3f91cSBen Skeggs 			NV_PRINTK(err, cli, "failed to idle channel %d [%s]\n",
79fcf3f91cSBen Skeggs 				  chan->chid, nvxx_client(&cli->base)->name);
80ebb945a9SBen Skeggs 			return ret;
81ebb945a9SBen Skeggs 		}
82fbd58ebdSBen Skeggs 	}
83fbd58ebdSBen Skeggs 	return 0;
84fbd58ebdSBen Skeggs }
85ebb945a9SBen Skeggs 
86ebb945a9SBen Skeggs void
87ebb945a9SBen Skeggs nouveau_channel_del(struct nouveau_channel **pchan)
88ebb945a9SBen Skeggs {
89ebb945a9SBen Skeggs 	struct nouveau_channel *chan = *pchan;
90ebb945a9SBen Skeggs 	if (chan) {
91d7722134SBen Skeggs 		struct nouveau_cli *cli = (void *)chan->user.client;
92d7722134SBen Skeggs 		bool super;
93d7722134SBen Skeggs 
94d7722134SBen Skeggs 		if (cli) {
95d7722134SBen Skeggs 			super = cli->base.super;
96d7722134SBen Skeggs 			cli->base.super = true;
97d7722134SBen Skeggs 		}
98d7722134SBen Skeggs 
99fbd58ebdSBen Skeggs 		if (chan->fence)
100ebb945a9SBen Skeggs 			nouveau_fence(chan->drm)->context_del(chan);
101eeaf06acSBen Skeggs 
102eeaf06acSBen Skeggs 		if (cli)
103eeaf06acSBen Skeggs 			nouveau_svmm_part(chan->vmm->svmm, chan->inst);
104eeaf06acSBen Skeggs 
1059ac596a4SBen Skeggs 		nvif_object_dtor(&chan->nvsw);
1069ac596a4SBen Skeggs 		nvif_object_dtor(&chan->gart);
1079ac596a4SBen Skeggs 		nvif_object_dtor(&chan->vram);
108f7a7d22aSBen Skeggs 		nvif_notify_dtor(&chan->kill);
1099ac596a4SBen Skeggs 		nvif_object_dtor(&chan->user);
1109ac596a4SBen Skeggs 		nvif_object_dtor(&chan->push.ctxdma);
11124e8375bSBen Skeggs 		nouveau_vma_del(&chan->push.vma);
112ebb945a9SBen Skeggs 		nouveau_bo_unmap(chan->push.buffer);
113124ea297SMarcin Slusarz 		if (chan->push.buffer && chan->push.buffer->pin_refcnt)
114124ea297SMarcin Slusarz 			nouveau_bo_unpin(chan->push.buffer);
115ebb945a9SBen Skeggs 		nouveau_bo_ref(NULL, &chan->push.buffer);
116ebb945a9SBen Skeggs 		kfree(chan);
117d7722134SBen Skeggs 
118d7722134SBen Skeggs 		if (cli)
119d7722134SBen Skeggs 			cli->base.super = super;
120ebb945a9SBen Skeggs 	}
121ebb945a9SBen Skeggs 	*pchan = NULL;
122ebb945a9SBen Skeggs }
123ebb945a9SBen Skeggs 
124*fdb06e2bSBen Skeggs static void
125*fdb06e2bSBen Skeggs nouveau_channel_kick(struct nvif_push *push)
126*fdb06e2bSBen Skeggs {
127*fdb06e2bSBen Skeggs 	struct nouveau_channel *chan = container_of(push, typeof(*chan), chan._push);
128*fdb06e2bSBen Skeggs 	chan->dma.cur = chan->dma.cur + (chan->chan._push.cur - chan->chan._push.bgn);
129*fdb06e2bSBen Skeggs 	FIRE_RING(chan);
130*fdb06e2bSBen Skeggs 	chan->chan._push.bgn = chan->chan._push.cur;
131*fdb06e2bSBen Skeggs }
132*fdb06e2bSBen Skeggs 
133*fdb06e2bSBen Skeggs static int
134*fdb06e2bSBen Skeggs nouveau_channel_wait(struct nvif_push *push, u32 size)
135*fdb06e2bSBen Skeggs {
136*fdb06e2bSBen Skeggs 	struct nouveau_channel *chan = container_of(push, typeof(*chan), chan._push);
137*fdb06e2bSBen Skeggs 	int ret;
138*fdb06e2bSBen Skeggs 	chan->dma.cur = chan->dma.cur + (chan->chan._push.cur - chan->chan._push.bgn);
139*fdb06e2bSBen Skeggs 	ret = RING_SPACE(chan, size);
140*fdb06e2bSBen Skeggs 	if (ret == 0) {
141*fdb06e2bSBen Skeggs 		chan->chan._push.bgn = chan->chan._push.mem.object.map.ptr;
142*fdb06e2bSBen Skeggs 		chan->chan._push.bgn = chan->chan._push.bgn + chan->dma.cur;
143*fdb06e2bSBen Skeggs 		chan->chan._push.cur = chan->chan._push.bgn;
144*fdb06e2bSBen Skeggs 		chan->chan._push.end = chan->chan._push.bgn + size;
145*fdb06e2bSBen Skeggs 	}
146*fdb06e2bSBen Skeggs 	return ret;
147*fdb06e2bSBen Skeggs }
148*fdb06e2bSBen Skeggs 
149ebb945a9SBen Skeggs static int
1500ad72863SBen Skeggs nouveau_channel_prep(struct nouveau_drm *drm, struct nvif_device *device,
151fcf3f91cSBen Skeggs 		     u32 size, struct nouveau_channel **pchan)
152ebb945a9SBen Skeggs {
153a01ca78cSBen Skeggs 	struct nouveau_cli *cli = (void *)device->object.client;
1544acfd707SBen Skeggs 	struct nv_dma_v0 args = {};
155ebb945a9SBen Skeggs 	struct nouveau_channel *chan;
156ebb945a9SBen Skeggs 	u32 target;
157ebb945a9SBen Skeggs 	int ret;
158ebb945a9SBen Skeggs 
159ebb945a9SBen Skeggs 	chan = *pchan = kzalloc(sizeof(*chan), GFP_KERNEL);
160ebb945a9SBen Skeggs 	if (!chan)
161ebb945a9SBen Skeggs 		return -ENOMEM;
162ebb945a9SBen Skeggs 
163a01ca78cSBen Skeggs 	chan->device = device;
164ebb945a9SBen Skeggs 	chan->drm = drm;
165bfe91afaSBen Skeggs 	chan->vmm = cli->svm.cli ? &cli->svm : &cli->vmm;
166d8cc37d8SBen Skeggs 	atomic_set(&chan->killed, 0);
167ebb945a9SBen Skeggs 
168ebb945a9SBen Skeggs 	/* allocate memory for dma push buffer */
169a81349a7SAlexandre Courbot 	target = TTM_PL_FLAG_TT | TTM_PL_FLAG_UNCACHED;
170ebb945a9SBen Skeggs 	if (nouveau_vram_pushbuf)
171ebb945a9SBen Skeggs 		target = TTM_PL_FLAG_VRAM;
172ebb945a9SBen Skeggs 
173bab7cc18SBen Skeggs 	ret = nouveau_bo_new(cli, size, 0, target, 0, 0, NULL, NULL,
174ebb945a9SBen Skeggs 			    &chan->push.buffer);
175ebb945a9SBen Skeggs 	if (ret == 0) {
176ad76b3f7SBen Skeggs 		ret = nouveau_bo_pin(chan->push.buffer, target, false);
177ebb945a9SBen Skeggs 		if (ret == 0)
178ebb945a9SBen Skeggs 			ret = nouveau_bo_map(chan->push.buffer);
179ebb945a9SBen Skeggs 	}
180ebb945a9SBen Skeggs 
181ebb945a9SBen Skeggs 	if (ret) {
182ebb945a9SBen Skeggs 		nouveau_channel_del(pchan);
183ebb945a9SBen Skeggs 		return ret;
184ebb945a9SBen Skeggs 	}
185ebb945a9SBen Skeggs 
186*fdb06e2bSBen Skeggs 	chan->chan._push.mem.object.parent = cli->base.object.parent;
187*fdb06e2bSBen Skeggs 	chan->chan._push.mem.object.client = &cli->base;
188*fdb06e2bSBen Skeggs 	chan->chan._push.mem.object.name = "chanPush";
189*fdb06e2bSBen Skeggs 	chan->chan._push.mem.object.map.ptr = chan->push.buffer->kmap.virtual;
190*fdb06e2bSBen Skeggs 	chan->chan._push.wait = nouveau_channel_wait;
191*fdb06e2bSBen Skeggs 	chan->chan._push.kick = nouveau_channel_kick;
192*fdb06e2bSBen Skeggs 	chan->chan.push = &chan->chan._push;
193*fdb06e2bSBen Skeggs 
194ebb945a9SBen Skeggs 	/* create dma object covering the *entire* memory space that the
195ebb945a9SBen Skeggs 	 * pushbuf lives in, this is because the GEM code requires that
196ebb945a9SBen Skeggs 	 * we be able to call out to other (indirect) push buffers
197ebb945a9SBen Skeggs 	 */
1980dc9b286SNirmoy Das 	chan->push.addr = chan->push.buffer->offset;
199ebb945a9SBen Skeggs 
200967e7bdeSBen Skeggs 	if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
201bfe91afaSBen Skeggs 		ret = nouveau_vma_new(chan->push.buffer, chan->vmm,
202ebb945a9SBen Skeggs 				      &chan->push.vma);
203ebb945a9SBen Skeggs 		if (ret) {
204ebb945a9SBen Skeggs 			nouveau_channel_del(pchan);
205ebb945a9SBen Skeggs 			return ret;
206ebb945a9SBen Skeggs 		}
207ebb945a9SBen Skeggs 
20892b4eaafSBen Skeggs 		chan->push.addr = chan->push.vma->addr;
20992b4eaafSBen Skeggs 
21092b4eaafSBen Skeggs 		if (device->info.family >= NV_DEVICE_INFO_V0_FERMI)
21192b4eaafSBen Skeggs 			return 0;
21292b4eaafSBen Skeggs 
2134acfd707SBen Skeggs 		args.target = NV_DMA_V0_TARGET_VM;
2144acfd707SBen Skeggs 		args.access = NV_DMA_V0_ACCESS_VM;
215ebb945a9SBen Skeggs 		args.start = 0;
216bfe91afaSBen Skeggs 		args.limit = chan->vmm->vmm.limit - 1;
217ebb945a9SBen Skeggs 	} else
218ebb945a9SBen Skeggs 	if (chan->push.buffer->bo.mem.mem_type == TTM_PL_VRAM) {
219967e7bdeSBen Skeggs 		if (device->info.family == NV_DEVICE_INFO_V0_TNT) {
220ebb945a9SBen Skeggs 			/* nv04 vram pushbuf hack, retarget to its location in
221ebb945a9SBen Skeggs 			 * the framebuffer bar rather than direct vram access..
222ebb945a9SBen Skeggs 			 * nfi why this exists, it came from the -nv ddx.
223ebb945a9SBen Skeggs 			 */
2244acfd707SBen Skeggs 			args.target = NV_DMA_V0_TARGET_PCI;
2254acfd707SBen Skeggs 			args.access = NV_DMA_V0_ACCESS_RDWR;
2267e8820feSBen Skeggs 			args.start = nvxx_device(device)->func->
2277e8820feSBen Skeggs 				resource_addr(nvxx_device(device), 1);
228f392ec4bSBen Skeggs 			args.limit = args.start + device->info.ram_user - 1;
229ebb945a9SBen Skeggs 		} else {
2304acfd707SBen Skeggs 			args.target = NV_DMA_V0_TARGET_VRAM;
2314acfd707SBen Skeggs 			args.access = NV_DMA_V0_ACCESS_RDWR;
232ebb945a9SBen Skeggs 			args.start = 0;
233f392ec4bSBen Skeggs 			args.limit = device->info.ram_user - 1;
234ebb945a9SBen Skeggs 		}
235ebb945a9SBen Skeggs 	} else {
236340b0e7cSBen Skeggs 		if (chan->drm->agp.bridge) {
2374acfd707SBen Skeggs 			args.target = NV_DMA_V0_TARGET_AGP;
2384acfd707SBen Skeggs 			args.access = NV_DMA_V0_ACCESS_RDWR;
239ebb945a9SBen Skeggs 			args.start = chan->drm->agp.base;
240ebb945a9SBen Skeggs 			args.limit = chan->drm->agp.base +
241ebb945a9SBen Skeggs 				     chan->drm->agp.size - 1;
242ebb945a9SBen Skeggs 		} else {
2434acfd707SBen Skeggs 			args.target = NV_DMA_V0_TARGET_VM;
2444acfd707SBen Skeggs 			args.access = NV_DMA_V0_ACCESS_RDWR;
245ebb945a9SBen Skeggs 			args.start = 0;
246bfe91afaSBen Skeggs 			args.limit = chan->vmm->vmm.limit - 1;
247ebb945a9SBen Skeggs 		}
248ebb945a9SBen Skeggs 	}
249ebb945a9SBen Skeggs 
2509ac596a4SBen Skeggs 	ret = nvif_object_ctor(&device->object, "abi16PushCtxDma", 0,
2519ac596a4SBen Skeggs 			       NV_DMA_FROM_MEMORY, &args, sizeof(args),
2529ac596a4SBen Skeggs 			       &chan->push.ctxdma);
253ebb945a9SBen Skeggs 	if (ret) {
254ebb945a9SBen Skeggs 		nouveau_channel_del(pchan);
255ebb945a9SBen Skeggs 		return ret;
256ebb945a9SBen Skeggs 	}
257ebb945a9SBen Skeggs 
258ebb945a9SBen Skeggs 	return 0;
259ebb945a9SBen Skeggs }
260ebb945a9SBen Skeggs 
2615b8a43aeSMarcin Slusarz static int
2620ad72863SBen Skeggs nouveau_channel_ind(struct nouveau_drm *drm, struct nvif_device *device,
26385532bd9SBen Skeggs 		    u64 runlist, bool priv, struct nouveau_channel **pchan)
264ebb945a9SBen Skeggs {
265641d0b30SBen Skeggs 	static const u16 oclasses[] = { TURING_CHANNEL_GPFIFO_A,
266641d0b30SBen Skeggs 					VOLTA_CHANNEL_GPFIFO_A,
26737e1c45aSBen Skeggs 					PASCAL_CHANNEL_GPFIFO_A,
268e8ff9794SBen Skeggs 					MAXWELL_CHANNEL_GPFIFO_A,
26963f8c9b7SBen Skeggs 					KEPLER_CHANNEL_GPFIFO_B,
270a1020afeSBen Skeggs 					KEPLER_CHANNEL_GPFIFO_A,
271bbf8906bSBen Skeggs 					FERMI_CHANNEL_GPFIFO,
272bbf8906bSBen Skeggs 					G82_CHANNEL_GPFIFO,
273bbf8906bSBen Skeggs 					NV50_CHANNEL_GPFIFO,
274c97f8c92SBen Skeggs 					0 };
275ebb945a9SBen Skeggs 	const u16 *oclass = oclasses;
276bbf8906bSBen Skeggs 	union {
277bbf8906bSBen Skeggs 		struct nv50_channel_gpfifo_v0 nv50;
278159045cdSBen Skeggs 		struct fermi_channel_gpfifo_v0 fermi;
279bbf8906bSBen Skeggs 		struct kepler_channel_gpfifo_a_v0 kepler;
2809d24907cSBen Skeggs 		struct volta_channel_gpfifo_a_v0 volta;
281a01ca78cSBen Skeggs 	} args;
282ebb945a9SBen Skeggs 	struct nouveau_channel *chan;
283bbf8906bSBen Skeggs 	u32 size;
284ebb945a9SBen Skeggs 	int ret;
285ebb945a9SBen Skeggs 
286ebb945a9SBen Skeggs 	/* allocate dma push buffer */
287fcf3f91cSBen Skeggs 	ret = nouveau_channel_prep(drm, device, 0x12000, &chan);
288ebb945a9SBen Skeggs 	*pchan = chan;
289ebb945a9SBen Skeggs 	if (ret)
290ebb945a9SBen Skeggs 		return ret;
291ebb945a9SBen Skeggs 
292ebb945a9SBen Skeggs 	/* create channel object */
293ebb945a9SBen Skeggs 	do {
2949d24907cSBen Skeggs 		if (oclass[0] >= VOLTA_CHANNEL_GPFIFO_A) {
2959d24907cSBen Skeggs 			args.volta.version = 0;
2969d24907cSBen Skeggs 			args.volta.ilength = 0x02000;
2979d24907cSBen Skeggs 			args.volta.ioffset = 0x10000 + chan->push.addr;
2989d24907cSBen Skeggs 			args.volta.runlist = runlist;
299bfe91afaSBen Skeggs 			args.volta.vmm = nvif_handle(&chan->vmm->vmm.object);
3009d24907cSBen Skeggs 			args.volta.priv = priv;
3019d24907cSBen Skeggs 			size = sizeof(args.volta);
3029d24907cSBen Skeggs 		} else
303bbf8906bSBen Skeggs 		if (oclass[0] >= KEPLER_CHANNEL_GPFIFO_A) {
304bbf8906bSBen Skeggs 			args.kepler.version = 0;
305bbf8906bSBen Skeggs 			args.kepler.ilength = 0x02000;
30624e8375bSBen Skeggs 			args.kepler.ioffset = 0x10000 + chan->push.addr;
307a7cf0180SBen Skeggs 			args.kepler.runlist = runlist;
308bfe91afaSBen Skeggs 			args.kepler.vmm = nvif_handle(&chan->vmm->vmm.object);
30985532bd9SBen Skeggs 			args.kepler.priv = priv;
310bbf8906bSBen Skeggs 			size = sizeof(args.kepler);
311159045cdSBen Skeggs 		} else
312159045cdSBen Skeggs 		if (oclass[0] >= FERMI_CHANNEL_GPFIFO) {
313159045cdSBen Skeggs 			args.fermi.version = 0;
314159045cdSBen Skeggs 			args.fermi.ilength = 0x02000;
31524e8375bSBen Skeggs 			args.fermi.ioffset = 0x10000 + chan->push.addr;
316bfe91afaSBen Skeggs 			args.fermi.vmm = nvif_handle(&chan->vmm->vmm.object);
317159045cdSBen Skeggs 			size = sizeof(args.fermi);
318bbf8906bSBen Skeggs 		} else {
319bbf8906bSBen Skeggs 			args.nv50.version = 0;
320bbf8906bSBen Skeggs 			args.nv50.ilength = 0x02000;
32124e8375bSBen Skeggs 			args.nv50.ioffset = 0x10000 + chan->push.addr;
322159045cdSBen Skeggs 			args.nv50.pushbuf = nvif_handle(&chan->push.ctxdma);
323bfe91afaSBen Skeggs 			args.nv50.vmm = nvif_handle(&chan->vmm->vmm.object);
324bbf8906bSBen Skeggs 			size = sizeof(args.nv50);
325bbf8906bSBen Skeggs 		}
326bbf8906bSBen Skeggs 
3279ac596a4SBen Skeggs 		ret = nvif_object_ctor(&device->object, "abi16ChanUser", 0,
3289ac596a4SBen Skeggs 				       *oclass++, &args, size, &chan->user);
329bbf8906bSBen Skeggs 		if (ret == 0) {
3309d24907cSBen Skeggs 			if (chan->user.oclass >= VOLTA_CHANNEL_GPFIFO_A) {
3319d24907cSBen Skeggs 				chan->chid = args.volta.chid;
3329d24907cSBen Skeggs 				chan->inst = args.volta.inst;
3339d24907cSBen Skeggs 				chan->token = args.volta.token;
3349d24907cSBen Skeggs 			} else
33586b442d7SBen Skeggs 			if (chan->user.oclass >= KEPLER_CHANNEL_GPFIFO_A) {
336a01ca78cSBen Skeggs 				chan->chid = args.kepler.chid;
33786b442d7SBen Skeggs 				chan->inst = args.kepler.inst;
33886b442d7SBen Skeggs 			} else
33986b442d7SBen Skeggs 			if (chan->user.oclass >= FERMI_CHANNEL_GPFIFO) {
340159045cdSBen Skeggs 				chan->chid = args.fermi.chid;
34186b442d7SBen Skeggs 			} else {
342a01ca78cSBen Skeggs 				chan->chid = args.nv50.chid;
34386b442d7SBen Skeggs 			}
344ebb945a9SBen Skeggs 			return ret;
345bbf8906bSBen Skeggs 		}
346ebb945a9SBen Skeggs 	} while (*oclass);
347ebb945a9SBen Skeggs 
348ebb945a9SBen Skeggs 	nouveau_channel_del(pchan);
349ebb945a9SBen Skeggs 	return ret;
350ebb945a9SBen Skeggs }
351ebb945a9SBen Skeggs 
352ebb945a9SBen Skeggs static int
3530ad72863SBen Skeggs nouveau_channel_dma(struct nouveau_drm *drm, struct nvif_device *device,
354fcf3f91cSBen Skeggs 		    struct nouveau_channel **pchan)
355ebb945a9SBen Skeggs {
356bbf8906bSBen Skeggs 	static const u16 oclasses[] = { NV40_CHANNEL_DMA,
357bbf8906bSBen Skeggs 					NV17_CHANNEL_DMA,
358bbf8906bSBen Skeggs 					NV10_CHANNEL_DMA,
359bbf8906bSBen Skeggs 					NV03_CHANNEL_DMA,
360c97f8c92SBen Skeggs 					0 };
361ebb945a9SBen Skeggs 	const u16 *oclass = oclasses;
362a01ca78cSBen Skeggs 	struct nv03_channel_dma_v0 args;
363ebb945a9SBen Skeggs 	struct nouveau_channel *chan;
364ebb945a9SBen Skeggs 	int ret;
365ebb945a9SBen Skeggs 
366ebb945a9SBen Skeggs 	/* allocate dma push buffer */
367fcf3f91cSBen Skeggs 	ret = nouveau_channel_prep(drm, device, 0x10000, &chan);
368ebb945a9SBen Skeggs 	*pchan = chan;
369ebb945a9SBen Skeggs 	if (ret)
370ebb945a9SBen Skeggs 		return ret;
371ebb945a9SBen Skeggs 
372ebb945a9SBen Skeggs 	/* create channel object */
373bbf8906bSBen Skeggs 	args.version = 0;
374bf81df9bSBen Skeggs 	args.pushbuf = nvif_handle(&chan->push.ctxdma);
37524e8375bSBen Skeggs 	args.offset = chan->push.addr;
376ebb945a9SBen Skeggs 
377ebb945a9SBen Skeggs 	do {
3789ac596a4SBen Skeggs 		ret = nvif_object_ctor(&device->object, "abi16ChanUser", 0,
3799ac596a4SBen Skeggs 				       *oclass++, &args, sizeof(args),
3809ac596a4SBen Skeggs 				       &chan->user);
381bbf8906bSBen Skeggs 		if (ret == 0) {
382a01ca78cSBen Skeggs 			chan->chid = args.chid;
383ebb945a9SBen Skeggs 			return ret;
384bbf8906bSBen Skeggs 		}
385ebb945a9SBen Skeggs 	} while (ret && *oclass);
386ebb945a9SBen Skeggs 
387ebb945a9SBen Skeggs 	nouveau_channel_del(pchan);
388ebb945a9SBen Skeggs 	return ret;
389ebb945a9SBen Skeggs }
390ebb945a9SBen Skeggs 
391ebb945a9SBen Skeggs static int
392ebb945a9SBen Skeggs nouveau_channel_init(struct nouveau_channel *chan, u32 vram, u32 gart)
393ebb945a9SBen Skeggs {
3940ad72863SBen Skeggs 	struct nvif_device *device = chan->device;
395d8cc37d8SBen Skeggs 	struct nouveau_drm *drm = chan->drm;
3964acfd707SBen Skeggs 	struct nv_dma_v0 args = {};
397ebb945a9SBen Skeggs 	int ret, i;
398ebb945a9SBen Skeggs 
39901326050SBen Skeggs 	nvif_object_map(&chan->user, NULL, 0);
4006c6ae061SBen Skeggs 
401d8cc37d8SBen Skeggs 	if (chan->user.oclass >= FERMI_CHANNEL_GPFIFO) {
402f7a7d22aSBen Skeggs 		ret = nvif_notify_ctor(&chan->user, "abi16ChanKilled",
403f7a7d22aSBen Skeggs 				       nouveau_channel_killed,
404d8cc37d8SBen Skeggs 				       true, NV906F_V0_NTFY_KILLED,
405d8cc37d8SBen Skeggs 				       NULL, 0, 0, &chan->kill);
406d8cc37d8SBen Skeggs 		if (ret == 0)
407d8cc37d8SBen Skeggs 			ret = nvif_notify_get(&chan->kill);
408d8cc37d8SBen Skeggs 		if (ret) {
409d8cc37d8SBen Skeggs 			NV_ERROR(drm, "Failed to request channel kill "
410d8cc37d8SBen Skeggs 				      "notification: %d\n", ret);
411d8cc37d8SBen Skeggs 			return ret;
412d8cc37d8SBen Skeggs 		}
413d8cc37d8SBen Skeggs 	}
414d8cc37d8SBen Skeggs 
415ebb945a9SBen Skeggs 	/* allocate dma objects to cover all allowed vram, and gart */
416967e7bdeSBen Skeggs 	if (device->info.family < NV_DEVICE_INFO_V0_FERMI) {
417967e7bdeSBen Skeggs 		if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
4184acfd707SBen Skeggs 			args.target = NV_DMA_V0_TARGET_VM;
4194acfd707SBen Skeggs 			args.access = NV_DMA_V0_ACCESS_VM;
420ebb945a9SBen Skeggs 			args.start = 0;
421bfe91afaSBen Skeggs 			args.limit = chan->vmm->vmm.limit - 1;
422ebb945a9SBen Skeggs 		} else {
4234acfd707SBen Skeggs 			args.target = NV_DMA_V0_TARGET_VRAM;
4244acfd707SBen Skeggs 			args.access = NV_DMA_V0_ACCESS_RDWR;
425ebb945a9SBen Skeggs 			args.start = 0;
426f392ec4bSBen Skeggs 			args.limit = device->info.ram_user - 1;
427ebb945a9SBen Skeggs 		}
428ebb945a9SBen Skeggs 
4299ac596a4SBen Skeggs 		ret = nvif_object_ctor(&chan->user, "abi16ChanVramCtxDma", vram,
4309ac596a4SBen Skeggs 				       NV_DMA_IN_MEMORY, &args, sizeof(args),
4319ac596a4SBen Skeggs 				       &chan->vram);
432ebb945a9SBen Skeggs 		if (ret)
433ebb945a9SBen Skeggs 			return ret;
434ebb945a9SBen Skeggs 
435967e7bdeSBen Skeggs 		if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
4364acfd707SBen Skeggs 			args.target = NV_DMA_V0_TARGET_VM;
4374acfd707SBen Skeggs 			args.access = NV_DMA_V0_ACCESS_VM;
438ebb945a9SBen Skeggs 			args.start = 0;
439bfe91afaSBen Skeggs 			args.limit = chan->vmm->vmm.limit - 1;
440ebb945a9SBen Skeggs 		} else
441340b0e7cSBen Skeggs 		if (chan->drm->agp.bridge) {
4424acfd707SBen Skeggs 			args.target = NV_DMA_V0_TARGET_AGP;
4434acfd707SBen Skeggs 			args.access = NV_DMA_V0_ACCESS_RDWR;
444ebb945a9SBen Skeggs 			args.start = chan->drm->agp.base;
445ebb945a9SBen Skeggs 			args.limit = chan->drm->agp.base +
446ebb945a9SBen Skeggs 				     chan->drm->agp.size - 1;
447ebb945a9SBen Skeggs 		} else {
4484acfd707SBen Skeggs 			args.target = NV_DMA_V0_TARGET_VM;
4494acfd707SBen Skeggs 			args.access = NV_DMA_V0_ACCESS_RDWR;
450ebb945a9SBen Skeggs 			args.start = 0;
451bfe91afaSBen Skeggs 			args.limit = chan->vmm->vmm.limit - 1;
452ebb945a9SBen Skeggs 		}
453ebb945a9SBen Skeggs 
4549ac596a4SBen Skeggs 		ret = nvif_object_ctor(&chan->user, "abi16ChanGartCtxDma", gart,
4559ac596a4SBen Skeggs 				       NV_DMA_IN_MEMORY, &args, sizeof(args),
4569ac596a4SBen Skeggs 				       &chan->gart);
457ebb945a9SBen Skeggs 		if (ret)
458ebb945a9SBen Skeggs 			return ret;
459ebb945a9SBen Skeggs 	}
460ebb945a9SBen Skeggs 
461ebb945a9SBen Skeggs 	/* initialise dma tracking parameters */
462a01ca78cSBen Skeggs 	switch (chan->user.oclass & 0x00ff) {
463503b0f1cSBen Skeggs 	case 0x006b:
464ebb945a9SBen Skeggs 	case 0x006e:
465ebb945a9SBen Skeggs 		chan->user_put = 0x40;
466ebb945a9SBen Skeggs 		chan->user_get = 0x44;
467ebb945a9SBen Skeggs 		chan->dma.max = (0x10000 / 4) - 2;
468ebb945a9SBen Skeggs 		break;
469ebb945a9SBen Skeggs 	default:
470ebb945a9SBen Skeggs 		chan->user_put = 0x40;
471ebb945a9SBen Skeggs 		chan->user_get = 0x44;
472ebb945a9SBen Skeggs 		chan->user_get_hi = 0x60;
473ebb945a9SBen Skeggs 		chan->dma.ib_base =  0x10000 / 4;
474ebb945a9SBen Skeggs 		chan->dma.ib_max  = (0x02000 / 8) - 1;
475ebb945a9SBen Skeggs 		chan->dma.ib_put  = 0;
476ebb945a9SBen Skeggs 		chan->dma.ib_free = chan->dma.ib_max - chan->dma.ib_put;
477ebb945a9SBen Skeggs 		chan->dma.max = chan->dma.ib_base;
478ebb945a9SBen Skeggs 		break;
479ebb945a9SBen Skeggs 	}
480ebb945a9SBen Skeggs 
481ebb945a9SBen Skeggs 	chan->dma.put = 0;
482ebb945a9SBen Skeggs 	chan->dma.cur = chan->dma.put;
483ebb945a9SBen Skeggs 	chan->dma.free = chan->dma.max - chan->dma.cur;
484ebb945a9SBen Skeggs 
485ebb945a9SBen Skeggs 	ret = RING_SPACE(chan, NOUVEAU_DMA_SKIPS);
486ebb945a9SBen Skeggs 	if (ret)
487ebb945a9SBen Skeggs 		return ret;
488ebb945a9SBen Skeggs 
489ebb945a9SBen Skeggs 	for (i = 0; i < NOUVEAU_DMA_SKIPS; i++)
490ebb945a9SBen Skeggs 		OUT_RING(chan, 0x00000000);
491ebb945a9SBen Skeggs 
49269a6146dSBen Skeggs 	/* allocate software object class (used for fences on <= nv05) */
493967e7bdeSBen Skeggs 	if (device->info.family < NV_DEVICE_INFO_V0_CELSIUS) {
4949ac596a4SBen Skeggs 		ret = nvif_object_ctor(&chan->user, "abi16NvswFence", 0x006e,
49508f7633cSBen Skeggs 				       NVIF_CLASS_SW_NV04,
4960ad72863SBen Skeggs 				       NULL, 0, &chan->nvsw);
497ebb945a9SBen Skeggs 		if (ret)
498ebb945a9SBen Skeggs 			return ret;
499ebb945a9SBen Skeggs 
500ebb945a9SBen Skeggs 		ret = RING_SPACE(chan, 2);
501ebb945a9SBen Skeggs 		if (ret)
502ebb945a9SBen Skeggs 			return ret;
503ebb945a9SBen Skeggs 
504ebb945a9SBen Skeggs 		BEGIN_NV04(chan, NvSubSw, 0x0000, 1);
505f45f55c4SBen Skeggs 		OUT_RING  (chan, chan->nvsw.handle);
506ebb945a9SBen Skeggs 		FIRE_RING (chan);
507ebb945a9SBen Skeggs 	}
508ebb945a9SBen Skeggs 
509ebb945a9SBen Skeggs 	/* initialise synchronisation */
5104894f662SBen Skeggs 	return nouveau_fence(chan->drm)->context_new(chan);
511ebb945a9SBen Skeggs }
512ebb945a9SBen Skeggs 
513ebb945a9SBen Skeggs int
5140ad72863SBen Skeggs nouveau_channel_new(struct nouveau_drm *drm, struct nvif_device *device,
51585532bd9SBen Skeggs 		    u32 arg0, u32 arg1, bool priv,
51685532bd9SBen Skeggs 		    struct nouveau_channel **pchan)
517ebb945a9SBen Skeggs {
518a01ca78cSBen Skeggs 	struct nouveau_cli *cli = (void *)device->object.client;
51967e26e41SBen Skeggs 	bool super;
520ebb945a9SBen Skeggs 	int ret;
521ebb945a9SBen Skeggs 
52267e26e41SBen Skeggs 	/* hack until fencenv50 is fixed, and agp access relaxed */
52367e26e41SBen Skeggs 	super = cli->base.super;
52467e26e41SBen Skeggs 	cli->base.super = true;
52567e26e41SBen Skeggs 
52685532bd9SBen Skeggs 	ret = nouveau_channel_ind(drm, device, arg0, priv, pchan);
527ebb945a9SBen Skeggs 	if (ret) {
5289ad97edeSBen Skeggs 		NV_PRINTK(dbg, cli, "ib channel create, %d\n", ret);
529fcf3f91cSBen Skeggs 		ret = nouveau_channel_dma(drm, device, pchan);
530ebb945a9SBen Skeggs 		if (ret) {
5319ad97edeSBen Skeggs 			NV_PRINTK(dbg, cli, "dma channel create, %d\n", ret);
53267e26e41SBen Skeggs 			goto done;
533ebb945a9SBen Skeggs 		}
534ebb945a9SBen Skeggs 	}
535ebb945a9SBen Skeggs 
53649981046SBen Skeggs 	ret = nouveau_channel_init(*pchan, arg0, arg1);
537ebb945a9SBen Skeggs 	if (ret) {
5389ad97edeSBen Skeggs 		NV_PRINTK(err, cli, "channel failed to initialise, %d\n", ret);
539ebb945a9SBen Skeggs 		nouveau_channel_del(pchan);
540ebb945a9SBen Skeggs 	}
541ebb945a9SBen Skeggs 
542eeaf06acSBen Skeggs 	ret = nouveau_svmm_join((*pchan)->vmm->svmm, (*pchan)->inst);
543eeaf06acSBen Skeggs 	if (ret)
544eeaf06acSBen Skeggs 		nouveau_channel_del(pchan);
545eeaf06acSBen Skeggs 
54667e26e41SBen Skeggs done:
54767e26e41SBen Skeggs 	cli->base.super = super;
54867e26e41SBen Skeggs 	return ret;
549ebb945a9SBen Skeggs }
550eb47db4fSBen Skeggs 
551eb47db4fSBen Skeggs int
552eb47db4fSBen Skeggs nouveau_channels_init(struct nouveau_drm *drm)
553eb47db4fSBen Skeggs {
554eb47db4fSBen Skeggs 	struct {
555eb47db4fSBen Skeggs 		struct nv_device_info_v1 m;
556eb47db4fSBen Skeggs 		struct {
557eb47db4fSBen Skeggs 			struct nv_device_info_v1_data channels;
558eb47db4fSBen Skeggs 		} v;
559eb47db4fSBen Skeggs 	} args = {
560eb47db4fSBen Skeggs 		.m.version = 1,
561eb47db4fSBen Skeggs 		.m.count = sizeof(args.v) / sizeof(args.v.channels),
562eb47db4fSBen Skeggs 		.v.channels.mthd = NV_DEVICE_FIFO_CHANNELS,
563eb47db4fSBen Skeggs 	};
564eb47db4fSBen Skeggs 	struct nvif_object *device = &drm->client.device.object;
565eb47db4fSBen Skeggs 	int ret;
566eb47db4fSBen Skeggs 
567eb47db4fSBen Skeggs 	ret = nvif_object_mthd(device, NV_DEVICE_V0_INFO, &args, sizeof(args));
568eb47db4fSBen Skeggs 	if (ret || args.v.channels.mthd == NV_DEVICE_INFO_INVALID)
569eb47db4fSBen Skeggs 		return -ENODEV;
570eb47db4fSBen Skeggs 
571eb47db4fSBen Skeggs 	drm->chan.nr = args.v.channels.data;
572eb47db4fSBen Skeggs 	drm->chan.context_base = dma_fence_context_alloc(drm->chan.nr);
573eb47db4fSBen Skeggs 	return 0;
574eb47db4fSBen Skeggs }
575