xref: /linux/drivers/gpu/drm/nouveau/nouveau_chan.c (revision 06db7fded6dec88772a65c5a39af12ba4dc2ad38)
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  */
24cd346a89SBen Skeggs #include <nvif/push006c.h>
25ebb945a9SBen Skeggs 
26fdb751efSBen Skeggs #include <nvif/class.h>
27845f2725SBen Skeggs #include <nvif/cl0002.h>
286c9705f6SBen Skeggs #include <nvif/if0020.h>
29fdb751efSBen Skeggs 
304dc28134SBen Skeggs #include "nouveau_drv.h"
31ebb945a9SBen Skeggs #include "nouveau_dma.h"
32ebb945a9SBen Skeggs #include "nouveau_bo.h"
33ebb945a9SBen Skeggs #include "nouveau_chan.h"
34ebb945a9SBen Skeggs #include "nouveau_fence.h"
35ebb945a9SBen Skeggs #include "nouveau_abi16.h"
3624e8375bSBen Skeggs #include "nouveau_vmm.h"
37eeaf06acSBen Skeggs #include "nouveau_svm.h"
38ebb945a9SBen Skeggs 
39ebb945a9SBen Skeggs MODULE_PARM_DESC(vram_pushbuf, "Create DMA push buffers in VRAM");
40703fa264SPierre Moreau int nouveau_vram_pushbuf;
41ebb945a9SBen Skeggs module_param_named(vram_pushbuf, nouveau_vram_pushbuf, int, 0400);
42ebb945a9SBen Skeggs 
43d8cc37d8SBen Skeggs static int
446c9705f6SBen Skeggs nouveau_channel_killed(struct nvif_event *event, void *repv, u32 repc)
45d8cc37d8SBen Skeggs {
466c9705f6SBen Skeggs 	struct nouveau_channel *chan = container_of(event, typeof(*chan), kill);
47d8cc37d8SBen Skeggs 	struct nouveau_cli *cli = (void *)chan->user.client;
486c9705f6SBen Skeggs 
49d8cc37d8SBen Skeggs 	NV_PRINTK(warn, cli, "channel %d killed!\n", chan->chid);
50d8cc37d8SBen Skeggs 	atomic_set(&chan->killed, 1);
51ea13e5abSBen Skeggs 	if (chan->fence)
52ea13e5abSBen Skeggs 		nouveau_fence_context_kill(chan->fence, -ENODEV);
536c9705f6SBen Skeggs 
546c9705f6SBen Skeggs 	return NVIF_EVENT_DROP;
55d8cc37d8SBen Skeggs }
56d8cc37d8SBen Skeggs 
57ebb945a9SBen Skeggs int
58ebb945a9SBen Skeggs nouveau_channel_idle(struct nouveau_channel *chan)
59ebb945a9SBen Skeggs {
6084cd0a55SBen Skeggs 	if (likely(chan && chan->fence && !atomic_read(&chan->killed))) {
61a01ca78cSBen Skeggs 		struct nouveau_cli *cli = (void *)chan->user.client;
62ebb945a9SBen Skeggs 		struct nouveau_fence *fence = NULL;
63ebb945a9SBen Skeggs 		int ret;
64ebb945a9SBen Skeggs 
65264ce192SBen Skeggs 		ret = nouveau_fence_new(chan, false, &fence);
66ebb945a9SBen Skeggs 		if (!ret) {
67ebb945a9SBen Skeggs 			ret = nouveau_fence_wait(fence, false, false);
68ebb945a9SBen Skeggs 			nouveau_fence_unref(&fence);
69ebb945a9SBen Skeggs 		}
70ebb945a9SBen Skeggs 
71fbd58ebdSBen Skeggs 		if (ret) {
72fcf3f91cSBen Skeggs 			NV_PRINTK(err, cli, "failed to idle channel %d [%s]\n",
73fcf3f91cSBen Skeggs 				  chan->chid, nvxx_client(&cli->base)->name);
74ebb945a9SBen Skeggs 			return ret;
75ebb945a9SBen Skeggs 		}
76fbd58ebdSBen Skeggs 	}
77fbd58ebdSBen Skeggs 	return 0;
78fbd58ebdSBen Skeggs }
79ebb945a9SBen Skeggs 
80ebb945a9SBen Skeggs void
81ebb945a9SBen Skeggs nouveau_channel_del(struct nouveau_channel **pchan)
82ebb945a9SBen Skeggs {
83ebb945a9SBen Skeggs 	struct nouveau_channel *chan = *pchan;
84ebb945a9SBen Skeggs 	if (chan) {
85d7722134SBen Skeggs 		struct nouveau_cli *cli = (void *)chan->user.client;
86d7722134SBen Skeggs 
87fbd58ebdSBen Skeggs 		if (chan->fence)
88ebb945a9SBen Skeggs 			nouveau_fence(chan->drm)->context_del(chan);
89eeaf06acSBen Skeggs 
90eeaf06acSBen Skeggs 		if (cli)
91eeaf06acSBen Skeggs 			nouveau_svmm_part(chan->vmm->svmm, chan->inst);
92eeaf06acSBen Skeggs 
939ac596a4SBen Skeggs 		nvif_object_dtor(&chan->nvsw);
949ac596a4SBen Skeggs 		nvif_object_dtor(&chan->gart);
959ac596a4SBen Skeggs 		nvif_object_dtor(&chan->vram);
966c9705f6SBen Skeggs 		nvif_event_dtor(&chan->kill);
979ac596a4SBen Skeggs 		nvif_object_dtor(&chan->user);
98*06db7fdeSBen Skeggs 		nvif_mem_dtor(&chan->mem_userd);
999ac596a4SBen Skeggs 		nvif_object_dtor(&chan->push.ctxdma);
10024e8375bSBen Skeggs 		nouveau_vma_del(&chan->push.vma);
101ebb945a9SBen Skeggs 		nouveau_bo_unmap(chan->push.buffer);
1026797cea1SChristian König 		if (chan->push.buffer && chan->push.buffer->bo.pin_count)
103124ea297SMarcin Slusarz 			nouveau_bo_unpin(chan->push.buffer);
104ebb945a9SBen Skeggs 		nouveau_bo_ref(NULL, &chan->push.buffer);
105ebb945a9SBen Skeggs 		kfree(chan);
106ebb945a9SBen Skeggs 	}
107ebb945a9SBen Skeggs 	*pchan = NULL;
108ebb945a9SBen Skeggs }
109ebb945a9SBen Skeggs 
110fdb06e2bSBen Skeggs static void
111fdb06e2bSBen Skeggs nouveau_channel_kick(struct nvif_push *push)
112fdb06e2bSBen Skeggs {
113fdb06e2bSBen Skeggs 	struct nouveau_channel *chan = container_of(push, typeof(*chan), chan._push);
114fdb06e2bSBen Skeggs 	chan->dma.cur = chan->dma.cur + (chan->chan._push.cur - chan->chan._push.bgn);
115fdb06e2bSBen Skeggs 	FIRE_RING(chan);
116fdb06e2bSBen Skeggs 	chan->chan._push.bgn = chan->chan._push.cur;
117fdb06e2bSBen Skeggs }
118fdb06e2bSBen Skeggs 
119fdb06e2bSBen Skeggs static int
120fdb06e2bSBen Skeggs nouveau_channel_wait(struct nvif_push *push, u32 size)
121fdb06e2bSBen Skeggs {
122fdb06e2bSBen Skeggs 	struct nouveau_channel *chan = container_of(push, typeof(*chan), chan._push);
123fdb06e2bSBen Skeggs 	int ret;
124fdb06e2bSBen Skeggs 	chan->dma.cur = chan->dma.cur + (chan->chan._push.cur - chan->chan._push.bgn);
125fdb06e2bSBen Skeggs 	ret = RING_SPACE(chan, size);
126fdb06e2bSBen Skeggs 	if (ret == 0) {
127fdb06e2bSBen Skeggs 		chan->chan._push.bgn = chan->chan._push.mem.object.map.ptr;
128fdb06e2bSBen Skeggs 		chan->chan._push.bgn = chan->chan._push.bgn + chan->dma.cur;
129fdb06e2bSBen Skeggs 		chan->chan._push.cur = chan->chan._push.bgn;
130fdb06e2bSBen Skeggs 		chan->chan._push.end = chan->chan._push.bgn + size;
131fdb06e2bSBen Skeggs 	}
132fdb06e2bSBen Skeggs 	return ret;
133fdb06e2bSBen Skeggs }
134fdb06e2bSBen Skeggs 
135ebb945a9SBen Skeggs static int
1360ad72863SBen Skeggs nouveau_channel_prep(struct nouveau_drm *drm, struct nvif_device *device,
137fcf3f91cSBen Skeggs 		     u32 size, struct nouveau_channel **pchan)
138ebb945a9SBen Skeggs {
139a01ca78cSBen Skeggs 	struct nouveau_cli *cli = (void *)device->object.client;
1404acfd707SBen Skeggs 	struct nv_dma_v0 args = {};
141ebb945a9SBen Skeggs 	struct nouveau_channel *chan;
142ebb945a9SBen Skeggs 	u32 target;
143ebb945a9SBen Skeggs 	int ret;
144ebb945a9SBen Skeggs 
145ebb945a9SBen Skeggs 	chan = *pchan = kzalloc(sizeof(*chan), GFP_KERNEL);
146ebb945a9SBen Skeggs 	if (!chan)
147ebb945a9SBen Skeggs 		return -ENOMEM;
148ebb945a9SBen Skeggs 
149a01ca78cSBen Skeggs 	chan->device = device;
150ebb945a9SBen Skeggs 	chan->drm = drm;
151bfe91afaSBen Skeggs 	chan->vmm = cli->svm.cli ? &cli->svm : &cli->vmm;
152d8cc37d8SBen Skeggs 	atomic_set(&chan->killed, 0);
153ebb945a9SBen Skeggs 
154ebb945a9SBen Skeggs 	/* allocate memory for dma push buffer */
15581b61579SChristian König 	target = NOUVEAU_GEM_DOMAIN_GART | NOUVEAU_GEM_DOMAIN_COHERENT;
156ebb945a9SBen Skeggs 	if (nouveau_vram_pushbuf)
15781b61579SChristian König 		target = NOUVEAU_GEM_DOMAIN_VRAM;
158ebb945a9SBen Skeggs 
159bab7cc18SBen Skeggs 	ret = nouveau_bo_new(cli, size, 0, target, 0, 0, NULL, NULL,
160ebb945a9SBen Skeggs 			    &chan->push.buffer);
161ebb945a9SBen Skeggs 	if (ret == 0) {
162ad76b3f7SBen Skeggs 		ret = nouveau_bo_pin(chan->push.buffer, target, false);
163ebb945a9SBen Skeggs 		if (ret == 0)
164ebb945a9SBen Skeggs 			ret = nouveau_bo_map(chan->push.buffer);
165ebb945a9SBen Skeggs 	}
166ebb945a9SBen Skeggs 
167ebb945a9SBen Skeggs 	if (ret) {
168ebb945a9SBen Skeggs 		nouveau_channel_del(pchan);
169ebb945a9SBen Skeggs 		return ret;
170ebb945a9SBen Skeggs 	}
171ebb945a9SBen Skeggs 
172fdb06e2bSBen Skeggs 	chan->chan._push.mem.object.parent = cli->base.object.parent;
173fdb06e2bSBen Skeggs 	chan->chan._push.mem.object.client = &cli->base;
174fdb06e2bSBen Skeggs 	chan->chan._push.mem.object.name = "chanPush";
175fdb06e2bSBen Skeggs 	chan->chan._push.mem.object.map.ptr = chan->push.buffer->kmap.virtual;
176fdb06e2bSBen Skeggs 	chan->chan._push.wait = nouveau_channel_wait;
177fdb06e2bSBen Skeggs 	chan->chan._push.kick = nouveau_channel_kick;
178fdb06e2bSBen Skeggs 	chan->chan.push = &chan->chan._push;
179fdb06e2bSBen Skeggs 
180ebb945a9SBen Skeggs 	/* create dma object covering the *entire* memory space that the
181ebb945a9SBen Skeggs 	 * pushbuf lives in, this is because the GEM code requires that
182ebb945a9SBen Skeggs 	 * we be able to call out to other (indirect) push buffers
183ebb945a9SBen Skeggs 	 */
1840dc9b286SNirmoy Das 	chan->push.addr = chan->push.buffer->offset;
185ebb945a9SBen Skeggs 
186967e7bdeSBen Skeggs 	if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
187bfe91afaSBen Skeggs 		ret = nouveau_vma_new(chan->push.buffer, chan->vmm,
188ebb945a9SBen Skeggs 				      &chan->push.vma);
189ebb945a9SBen Skeggs 		if (ret) {
190ebb945a9SBen Skeggs 			nouveau_channel_del(pchan);
191ebb945a9SBen Skeggs 			return ret;
192ebb945a9SBen Skeggs 		}
193ebb945a9SBen Skeggs 
19492b4eaafSBen Skeggs 		chan->push.addr = chan->push.vma->addr;
19592b4eaafSBen Skeggs 
19692b4eaafSBen Skeggs 		if (device->info.family >= NV_DEVICE_INFO_V0_FERMI)
19792b4eaafSBen Skeggs 			return 0;
19892b4eaafSBen Skeggs 
1994acfd707SBen Skeggs 		args.target = NV_DMA_V0_TARGET_VM;
2004acfd707SBen Skeggs 		args.access = NV_DMA_V0_ACCESS_VM;
201ebb945a9SBen Skeggs 		args.start = 0;
202bfe91afaSBen Skeggs 		args.limit = chan->vmm->vmm.limit - 1;
203ebb945a9SBen Skeggs 	} else
204d3116756SChristian König 	if (chan->push.buffer->bo.resource->mem_type == TTM_PL_VRAM) {
205967e7bdeSBen Skeggs 		if (device->info.family == NV_DEVICE_INFO_V0_TNT) {
206ebb945a9SBen Skeggs 			/* nv04 vram pushbuf hack, retarget to its location in
207ebb945a9SBen Skeggs 			 * the framebuffer bar rather than direct vram access..
208ebb945a9SBen Skeggs 			 * nfi why this exists, it came from the -nv ddx.
209ebb945a9SBen Skeggs 			 */
2104acfd707SBen Skeggs 			args.target = NV_DMA_V0_TARGET_PCI;
2114acfd707SBen Skeggs 			args.access = NV_DMA_V0_ACCESS_RDWR;
2127e8820feSBen Skeggs 			args.start = nvxx_device(device)->func->
2137e8820feSBen Skeggs 				resource_addr(nvxx_device(device), 1);
214f392ec4bSBen Skeggs 			args.limit = args.start + device->info.ram_user - 1;
215ebb945a9SBen Skeggs 		} else {
2164acfd707SBen Skeggs 			args.target = NV_DMA_V0_TARGET_VRAM;
2174acfd707SBen Skeggs 			args.access = NV_DMA_V0_ACCESS_RDWR;
218ebb945a9SBen Skeggs 			args.start = 0;
219f392ec4bSBen Skeggs 			args.limit = device->info.ram_user - 1;
220ebb945a9SBen Skeggs 		}
221ebb945a9SBen Skeggs 	} else {
222340b0e7cSBen Skeggs 		if (chan->drm->agp.bridge) {
2234acfd707SBen Skeggs 			args.target = NV_DMA_V0_TARGET_AGP;
2244acfd707SBen Skeggs 			args.access = NV_DMA_V0_ACCESS_RDWR;
225ebb945a9SBen Skeggs 			args.start = chan->drm->agp.base;
226ebb945a9SBen Skeggs 			args.limit = chan->drm->agp.base +
227ebb945a9SBen Skeggs 				     chan->drm->agp.size - 1;
228ebb945a9SBen Skeggs 		} else {
2294acfd707SBen Skeggs 			args.target = NV_DMA_V0_TARGET_VM;
2304acfd707SBen Skeggs 			args.access = NV_DMA_V0_ACCESS_RDWR;
231ebb945a9SBen Skeggs 			args.start = 0;
232bfe91afaSBen Skeggs 			args.limit = chan->vmm->vmm.limit - 1;
233ebb945a9SBen Skeggs 		}
234ebb945a9SBen Skeggs 	}
235ebb945a9SBen Skeggs 
2369ac596a4SBen Skeggs 	ret = nvif_object_ctor(&device->object, "abi16PushCtxDma", 0,
2379ac596a4SBen Skeggs 			       NV_DMA_FROM_MEMORY, &args, sizeof(args),
2389ac596a4SBen Skeggs 			       &chan->push.ctxdma);
239ebb945a9SBen Skeggs 	if (ret) {
240ebb945a9SBen Skeggs 		nouveau_channel_del(pchan);
241ebb945a9SBen Skeggs 		return ret;
242ebb945a9SBen Skeggs 	}
243ebb945a9SBen Skeggs 
244ebb945a9SBen Skeggs 	return 0;
245ebb945a9SBen Skeggs }
246ebb945a9SBen Skeggs 
2475b8a43aeSMarcin Slusarz static int
248*06db7fdeSBen Skeggs nouveau_channel_ctor(struct nouveau_drm *drm, struct nvif_device *device, bool priv, u64 runm,
249fcf3f91cSBen Skeggs 		     struct nouveau_channel **pchan)
250ebb945a9SBen Skeggs {
251*06db7fdeSBen Skeggs 	static const struct {
252*06db7fdeSBen Skeggs 		s32 oclass;
253*06db7fdeSBen Skeggs 		int version;
254*06db7fdeSBen Skeggs 	} hosts[] = {
255*06db7fdeSBen Skeggs 		{  AMPERE_CHANNEL_GPFIFO_B, 0 },
256*06db7fdeSBen Skeggs 		{  TURING_CHANNEL_GPFIFO_A, 0 },
257*06db7fdeSBen Skeggs 		{   VOLTA_CHANNEL_GPFIFO_A, 0 },
258*06db7fdeSBen Skeggs 		{  PASCAL_CHANNEL_GPFIFO_A, 0 },
259*06db7fdeSBen Skeggs 		{ MAXWELL_CHANNEL_GPFIFO_A, 0 },
260*06db7fdeSBen Skeggs 		{  KEPLER_CHANNEL_GPFIFO_B, 0 },
261*06db7fdeSBen Skeggs 		{  KEPLER_CHANNEL_GPFIFO_A, 0 },
262*06db7fdeSBen Skeggs 		{   FERMI_CHANNEL_GPFIFO  , 0 },
263*06db7fdeSBen Skeggs 		{     G82_CHANNEL_GPFIFO  , 0 },
264*06db7fdeSBen Skeggs 		{    NV50_CHANNEL_GPFIFO  , 0 },
265*06db7fdeSBen Skeggs 		{    NV40_CHANNEL_DMA     , 0 },
266*06db7fdeSBen Skeggs 		{    NV17_CHANNEL_DMA     , 0 },
267*06db7fdeSBen Skeggs 		{    NV10_CHANNEL_DMA     , 0 },
268*06db7fdeSBen Skeggs 		{    NV03_CHANNEL_DMA     , 0 },
269*06db7fdeSBen Skeggs 		{}
270*06db7fdeSBen Skeggs 	};
271*06db7fdeSBen Skeggs 	struct {
272*06db7fdeSBen Skeggs 		struct nvif_chan_v0 chan;
273*06db7fdeSBen Skeggs 		char name[TASK_COMM_LEN+16];
274*06db7fdeSBen Skeggs 	} args;
275*06db7fdeSBen Skeggs 	struct nouveau_cli *cli = (void *)device->object.client;
276ebb945a9SBen Skeggs 	struct nouveau_channel *chan;
277*06db7fdeSBen Skeggs 	const u64 plength = 0x10000;
278*06db7fdeSBen Skeggs 	const u64 ioffset = plength;
279*06db7fdeSBen Skeggs 	const u64 ilength = 0x02000;
280*06db7fdeSBen Skeggs 	char name[TASK_COMM_LEN];
281*06db7fdeSBen Skeggs 	int cid, ret;
282*06db7fdeSBen Skeggs 	u64 size;
283*06db7fdeSBen Skeggs 
284*06db7fdeSBen Skeggs 	cid = nvif_mclass(&device->object, hosts);
285*06db7fdeSBen Skeggs 	if (cid < 0)
286*06db7fdeSBen Skeggs 		return cid;
287*06db7fdeSBen Skeggs 
288*06db7fdeSBen Skeggs 	if (hosts[cid].oclass < NV50_CHANNEL_GPFIFO)
289*06db7fdeSBen Skeggs 		size = plength;
290*06db7fdeSBen Skeggs 	else
291*06db7fdeSBen Skeggs 		size = ioffset + ilength;
292ebb945a9SBen Skeggs 
293ebb945a9SBen Skeggs 	/* allocate dma push buffer */
294*06db7fdeSBen Skeggs 	ret = nouveau_channel_prep(drm, device, size, &chan);
295ebb945a9SBen Skeggs 	*pchan = chan;
296ebb945a9SBen Skeggs 	if (ret)
297ebb945a9SBen Skeggs 		return ret;
298ebb945a9SBen Skeggs 
299ebb945a9SBen Skeggs 	/* create channel object */
300*06db7fdeSBen Skeggs 	args.chan.version = 0;
301*06db7fdeSBen Skeggs 	args.chan.namelen = sizeof(args.name);
302*06db7fdeSBen Skeggs 	args.chan.runlist = __ffs64(runm);
303*06db7fdeSBen Skeggs 	args.chan.runq = 0;
304*06db7fdeSBen Skeggs 	args.chan.priv = priv;
305*06db7fdeSBen Skeggs 	args.chan.devm = BIT(0);
306*06db7fdeSBen Skeggs 	if (hosts[cid].oclass < NV50_CHANNEL_GPFIFO) {
307*06db7fdeSBen Skeggs 		args.chan.vmm = 0;
308*06db7fdeSBen Skeggs 		args.chan.ctxdma = nvif_handle(&chan->push.ctxdma);
309*06db7fdeSBen Skeggs 		args.chan.offset = chan->push.addr;
310*06db7fdeSBen Skeggs 		args.chan.length = 0;
311*06db7fdeSBen Skeggs 	} else {
312*06db7fdeSBen Skeggs 		args.chan.vmm = nvif_handle(&chan->vmm->vmm.object);
313*06db7fdeSBen Skeggs 		if (hosts[cid].oclass < FERMI_CHANNEL_GPFIFO)
314*06db7fdeSBen Skeggs 			args.chan.ctxdma = nvif_handle(&chan->push.ctxdma);
315*06db7fdeSBen Skeggs 		else
316*06db7fdeSBen Skeggs 			args.chan.ctxdma = 0;
317*06db7fdeSBen Skeggs 		args.chan.offset = ioffset + chan->push.addr;
318*06db7fdeSBen Skeggs 		args.chan.length = ilength;
319bbf8906bSBen Skeggs 	}
320*06db7fdeSBen Skeggs 	args.chan.huserd = 0;
321*06db7fdeSBen Skeggs 	args.chan.ouserd = 0;
322ebb945a9SBen Skeggs 
323*06db7fdeSBen Skeggs 	/* allocate userd */
324*06db7fdeSBen Skeggs 	if (hosts[cid].oclass >= VOLTA_CHANNEL_GPFIFO_A) {
325*06db7fdeSBen Skeggs 		ret = nvif_mem_ctor(&cli->mmu, "abi16ChanUSERD", NVIF_CLASS_MEM_GF100,
326*06db7fdeSBen Skeggs 				    NVIF_MEM_VRAM | NVIF_MEM_COHERENT | NVIF_MEM_MAPPABLE,
327*06db7fdeSBen Skeggs 				    0, PAGE_SIZE, NULL, 0, &chan->mem_userd);
328*06db7fdeSBen Skeggs 		if (ret)
329*06db7fdeSBen Skeggs 			return ret;
330*06db7fdeSBen Skeggs 
331*06db7fdeSBen Skeggs 		args.chan.huserd = nvif_handle(&chan->mem_userd.object);
332*06db7fdeSBen Skeggs 		args.chan.ouserd = 0;
333*06db7fdeSBen Skeggs 
334*06db7fdeSBen Skeggs 		chan->userd = &chan->mem_userd.object;
335*06db7fdeSBen Skeggs 	} else {
336*06db7fdeSBen Skeggs 		chan->userd = &chan->user;
337*06db7fdeSBen Skeggs 	}
338*06db7fdeSBen Skeggs 
339*06db7fdeSBen Skeggs 	get_task_comm(name, current);
340*06db7fdeSBen Skeggs 	snprintf(args.name, sizeof(args.name), "%s[%d]", name, task_pid_nr(current));
341*06db7fdeSBen Skeggs 
342*06db7fdeSBen Skeggs 	ret = nvif_object_ctor(&device->object, "abi16ChanUser", 0, hosts[cid].oclass,
343*06db7fdeSBen Skeggs 			       &args, sizeof(args), &chan->user);
344*06db7fdeSBen Skeggs 	if (ret) {
345ebb945a9SBen Skeggs 		nouveau_channel_del(pchan);
346ebb945a9SBen Skeggs 		return ret;
347ebb945a9SBen Skeggs 	}
348ebb945a9SBen Skeggs 
349*06db7fdeSBen Skeggs 	chan->runlist = args.chan.runlist;
350*06db7fdeSBen Skeggs 	chan->chid = args.chan.chid;
351*06db7fdeSBen Skeggs 	chan->inst = args.chan.inst;
352*06db7fdeSBen Skeggs 	chan->token = args.chan.token;
353*06db7fdeSBen Skeggs 	return 0;
354*06db7fdeSBen Skeggs }
355*06db7fdeSBen Skeggs 
356ebb945a9SBen Skeggs static int
357ebb945a9SBen Skeggs nouveau_channel_init(struct nouveau_channel *chan, u32 vram, u32 gart)
358ebb945a9SBen Skeggs {
3590ad72863SBen Skeggs 	struct nvif_device *device = chan->device;
360d8cc37d8SBen Skeggs 	struct nouveau_drm *drm = chan->drm;
3614acfd707SBen Skeggs 	struct nv_dma_v0 args = {};
362ebb945a9SBen Skeggs 	int ret, i;
363ebb945a9SBen Skeggs 
364*06db7fdeSBen Skeggs 	ret = nvif_object_map(chan->userd, NULL, 0);
365097d56cdSBen Skeggs 	if (ret)
366097d56cdSBen Skeggs 		return ret;
3676c6ae061SBen Skeggs 
36849b2dfc0SBen Skeggs 	if (chan->user.oclass >= FERMI_CHANNEL_GPFIFO &&
36949b2dfc0SBen Skeggs 	    chan->user.oclass < AMPERE_CHANNEL_GPFIFO_B) {
3706c9705f6SBen Skeggs 		struct {
3716c9705f6SBen Skeggs 			struct nvif_event_v0 base;
3726c9705f6SBen Skeggs 			struct nvif_chan_event_v0 host;
3736c9705f6SBen Skeggs 		} args;
3746c9705f6SBen Skeggs 
3756c9705f6SBen Skeggs 		args.host.version = 0;
3766c9705f6SBen Skeggs 		args.host.type = NVIF_CHAN_EVENT_V0_KILLED;
3776c9705f6SBen Skeggs 
3786c9705f6SBen Skeggs 		ret = nvif_event_ctor(&chan->user, "abi16ChanKilled", chan->chid,
3796c9705f6SBen Skeggs 				      nouveau_channel_killed, false,
3806c9705f6SBen Skeggs 				      &args.base, sizeof(args), &chan->kill);
381d8cc37d8SBen Skeggs 		if (ret == 0)
3826c9705f6SBen Skeggs 			ret = nvif_event_allow(&chan->kill);
383d8cc37d8SBen Skeggs 		if (ret) {
384d8cc37d8SBen Skeggs 			NV_ERROR(drm, "Failed to request channel kill "
385d8cc37d8SBen Skeggs 				      "notification: %d\n", ret);
386d8cc37d8SBen Skeggs 			return ret;
387d8cc37d8SBen Skeggs 		}
388d8cc37d8SBen Skeggs 	}
389d8cc37d8SBen Skeggs 
390ebb945a9SBen Skeggs 	/* allocate dma objects to cover all allowed vram, and gart */
391967e7bdeSBen Skeggs 	if (device->info.family < NV_DEVICE_INFO_V0_FERMI) {
392967e7bdeSBen Skeggs 		if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
3934acfd707SBen Skeggs 			args.target = NV_DMA_V0_TARGET_VM;
3944acfd707SBen Skeggs 			args.access = NV_DMA_V0_ACCESS_VM;
395ebb945a9SBen Skeggs 			args.start = 0;
396bfe91afaSBen Skeggs 			args.limit = chan->vmm->vmm.limit - 1;
397ebb945a9SBen Skeggs 		} else {
3984acfd707SBen Skeggs 			args.target = NV_DMA_V0_TARGET_VRAM;
3994acfd707SBen Skeggs 			args.access = NV_DMA_V0_ACCESS_RDWR;
400ebb945a9SBen Skeggs 			args.start = 0;
401f392ec4bSBen Skeggs 			args.limit = device->info.ram_user - 1;
402ebb945a9SBen Skeggs 		}
403ebb945a9SBen Skeggs 
4049ac596a4SBen Skeggs 		ret = nvif_object_ctor(&chan->user, "abi16ChanVramCtxDma", vram,
4059ac596a4SBen Skeggs 				       NV_DMA_IN_MEMORY, &args, sizeof(args),
4069ac596a4SBen Skeggs 				       &chan->vram);
407ebb945a9SBen Skeggs 		if (ret)
408ebb945a9SBen Skeggs 			return ret;
409ebb945a9SBen Skeggs 
410967e7bdeSBen Skeggs 		if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
4114acfd707SBen Skeggs 			args.target = NV_DMA_V0_TARGET_VM;
4124acfd707SBen Skeggs 			args.access = NV_DMA_V0_ACCESS_VM;
413ebb945a9SBen Skeggs 			args.start = 0;
414bfe91afaSBen Skeggs 			args.limit = chan->vmm->vmm.limit - 1;
415ebb945a9SBen Skeggs 		} else
416340b0e7cSBen Skeggs 		if (chan->drm->agp.bridge) {
4174acfd707SBen Skeggs 			args.target = NV_DMA_V0_TARGET_AGP;
4184acfd707SBen Skeggs 			args.access = NV_DMA_V0_ACCESS_RDWR;
419ebb945a9SBen Skeggs 			args.start = chan->drm->agp.base;
420ebb945a9SBen Skeggs 			args.limit = chan->drm->agp.base +
421ebb945a9SBen Skeggs 				     chan->drm->agp.size - 1;
422ebb945a9SBen Skeggs 		} else {
4234acfd707SBen Skeggs 			args.target = NV_DMA_V0_TARGET_VM;
4244acfd707SBen Skeggs 			args.access = NV_DMA_V0_ACCESS_RDWR;
425ebb945a9SBen Skeggs 			args.start = 0;
426bfe91afaSBen Skeggs 			args.limit = chan->vmm->vmm.limit - 1;
427ebb945a9SBen Skeggs 		}
428ebb945a9SBen Skeggs 
4299ac596a4SBen Skeggs 		ret = nvif_object_ctor(&chan->user, "abi16ChanGartCtxDma", gart,
4309ac596a4SBen Skeggs 				       NV_DMA_IN_MEMORY, &args, sizeof(args),
4319ac596a4SBen Skeggs 				       &chan->gart);
432ebb945a9SBen Skeggs 		if (ret)
433ebb945a9SBen Skeggs 			return ret;
434ebb945a9SBen Skeggs 	}
435ebb945a9SBen Skeggs 
436ebb945a9SBen Skeggs 	/* initialise dma tracking parameters */
437a01ca78cSBen Skeggs 	switch (chan->user.oclass & 0x00ff) {
438503b0f1cSBen Skeggs 	case 0x006b:
439ebb945a9SBen Skeggs 	case 0x006e:
440ebb945a9SBen Skeggs 		chan->user_put = 0x40;
441ebb945a9SBen Skeggs 		chan->user_get = 0x44;
442ebb945a9SBen Skeggs 		chan->dma.max = (0x10000 / 4) - 2;
443ebb945a9SBen Skeggs 		break;
444ebb945a9SBen Skeggs 	default:
445ebb945a9SBen Skeggs 		chan->user_put = 0x40;
446ebb945a9SBen Skeggs 		chan->user_get = 0x44;
447ebb945a9SBen Skeggs 		chan->user_get_hi = 0x60;
448ebb945a9SBen Skeggs 		chan->dma.ib_base =  0x10000 / 4;
449ebb945a9SBen Skeggs 		chan->dma.ib_max  = (0x02000 / 8) - 1;
450ebb945a9SBen Skeggs 		chan->dma.ib_put  = 0;
451ebb945a9SBen Skeggs 		chan->dma.ib_free = chan->dma.ib_max - chan->dma.ib_put;
452ebb945a9SBen Skeggs 		chan->dma.max = chan->dma.ib_base;
453ebb945a9SBen Skeggs 		break;
454ebb945a9SBen Skeggs 	}
455ebb945a9SBen Skeggs 
456ebb945a9SBen Skeggs 	chan->dma.put = 0;
457ebb945a9SBen Skeggs 	chan->dma.cur = chan->dma.put;
458ebb945a9SBen Skeggs 	chan->dma.free = chan->dma.max - chan->dma.cur;
459ebb945a9SBen Skeggs 
460cd346a89SBen Skeggs 	ret = PUSH_WAIT(chan->chan.push, NOUVEAU_DMA_SKIPS);
461ebb945a9SBen Skeggs 	if (ret)
462ebb945a9SBen Skeggs 		return ret;
463ebb945a9SBen Skeggs 
464ebb945a9SBen Skeggs 	for (i = 0; i < NOUVEAU_DMA_SKIPS; i++)
465cd346a89SBen Skeggs 		PUSH_DATA(chan->chan.push, 0x00000000);
466ebb945a9SBen Skeggs 
46769a6146dSBen Skeggs 	/* allocate software object class (used for fences on <= nv05) */
468967e7bdeSBen Skeggs 	if (device->info.family < NV_DEVICE_INFO_V0_CELSIUS) {
4699ac596a4SBen Skeggs 		ret = nvif_object_ctor(&chan->user, "abi16NvswFence", 0x006e,
47008f7633cSBen Skeggs 				       NVIF_CLASS_SW_NV04,
4710ad72863SBen Skeggs 				       NULL, 0, &chan->nvsw);
472ebb945a9SBen Skeggs 		if (ret)
473ebb945a9SBen Skeggs 			return ret;
474ebb945a9SBen Skeggs 
475cd346a89SBen Skeggs 		ret = PUSH_WAIT(chan->chan.push, 2);
476ebb945a9SBen Skeggs 		if (ret)
477ebb945a9SBen Skeggs 			return ret;
478ebb945a9SBen Skeggs 
479cd346a89SBen Skeggs 		PUSH_NVSQ(chan->chan.push, NV_SW, 0x0000, chan->nvsw.handle);
480cd346a89SBen Skeggs 		PUSH_KICK(chan->chan.push);
481ebb945a9SBen Skeggs 	}
482ebb945a9SBen Skeggs 
483ebb945a9SBen Skeggs 	/* initialise synchronisation */
4844894f662SBen Skeggs 	return nouveau_fence(chan->drm)->context_new(chan);
485ebb945a9SBen Skeggs }
486ebb945a9SBen Skeggs 
487ebb945a9SBen Skeggs int
4880ad72863SBen Skeggs nouveau_channel_new(struct nouveau_drm *drm, struct nvif_device *device,
4896de12538SBen Skeggs 		    bool priv, u64 runm, u32 vram, u32 gart, struct nouveau_channel **pchan)
490ebb945a9SBen Skeggs {
491a01ca78cSBen Skeggs 	struct nouveau_cli *cli = (void *)device->object.client;
492ebb945a9SBen Skeggs 	int ret;
493ebb945a9SBen Skeggs 
494*06db7fdeSBen Skeggs 	ret = nouveau_channel_ctor(drm, device, priv, runm, pchan);
495ebb945a9SBen Skeggs 	if (ret) {
496*06db7fdeSBen Skeggs 		NV_PRINTK(dbg, cli, "channel create, %d\n", ret);
49759f216cfSBen Skeggs 		return ret;
498ebb945a9SBen Skeggs 	}
499ebb945a9SBen Skeggs 
5006de12538SBen Skeggs 	ret = nouveau_channel_init(*pchan, vram, gart);
501ebb945a9SBen Skeggs 	if (ret) {
5029ad97edeSBen Skeggs 		NV_PRINTK(err, cli, "channel failed to initialise, %d\n", ret);
503ebb945a9SBen Skeggs 		nouveau_channel_del(pchan);
50459f216cfSBen Skeggs 		return ret;
505ebb945a9SBen Skeggs 	}
506ebb945a9SBen Skeggs 
507eeaf06acSBen Skeggs 	ret = nouveau_svmm_join((*pchan)->vmm->svmm, (*pchan)->inst);
508eeaf06acSBen Skeggs 	if (ret)
509eeaf06acSBen Skeggs 		nouveau_channel_del(pchan);
510eeaf06acSBen Skeggs 
51167e26e41SBen Skeggs 	return ret;
512ebb945a9SBen Skeggs }
513eb47db4fSBen Skeggs 
514eb39c613SBen Skeggs void
515eb39c613SBen Skeggs nouveau_channels_fini(struct nouveau_drm *drm)
516eb39c613SBen Skeggs {
517eb39c613SBen Skeggs 	kfree(drm->runl);
518eb39c613SBen Skeggs }
519eb39c613SBen Skeggs 
520eb47db4fSBen Skeggs int
521eb47db4fSBen Skeggs nouveau_channels_init(struct nouveau_drm *drm)
522eb47db4fSBen Skeggs {
523eb47db4fSBen Skeggs 	struct {
524eb47db4fSBen Skeggs 		struct nv_device_info_v1 m;
525eb47db4fSBen Skeggs 		struct {
526eb47db4fSBen Skeggs 			struct nv_device_info_v1_data channels;
527eb39c613SBen Skeggs 			struct nv_device_info_v1_data runlists;
528eb47db4fSBen Skeggs 		} v;
529eb47db4fSBen Skeggs 	} args = {
530eb47db4fSBen Skeggs 		.m.version = 1,
531eb47db4fSBen Skeggs 		.m.count = sizeof(args.v) / sizeof(args.v.channels),
532f8fabd31SBen Skeggs 		.v.channels.mthd = NV_DEVICE_HOST_CHANNELS,
533eb39c613SBen Skeggs 		.v.runlists.mthd = NV_DEVICE_HOST_RUNLISTS,
534eb47db4fSBen Skeggs 	};
535eb47db4fSBen Skeggs 	struct nvif_object *device = &drm->client.device.object;
536eb39c613SBen Skeggs 	int ret, i;
537eb39c613SBen Skeggs 
538eb39c613SBen Skeggs 	ret = nvif_object_mthd(device, NV_DEVICE_V0_INFO, &args, sizeof(args));
539eb39c613SBen Skeggs 	if (ret ||
540eb39c613SBen Skeggs 	    args.v.runlists.mthd == NV_DEVICE_INFO_INVALID || !args.v.runlists.data ||
541eb39c613SBen Skeggs 	    args.v.channels.mthd == NV_DEVICE_INFO_INVALID)
542eb39c613SBen Skeggs 		return -ENODEV;
543eb39c613SBen Skeggs 
544eb39c613SBen Skeggs 	drm->chan_nr = drm->chan_total = args.v.channels.data;
545eb39c613SBen Skeggs 	drm->runl_nr = fls64(args.v.runlists.data);
546eb39c613SBen Skeggs 	drm->runl = kcalloc(drm->runl_nr, sizeof(*drm->runl), GFP_KERNEL);
547eb39c613SBen Skeggs 	if (!drm->runl)
548eb39c613SBen Skeggs 		return -ENOMEM;
549eb39c613SBen Skeggs 
550eb39c613SBen Skeggs 	if (drm->chan_nr == 0) {
551eb39c613SBen Skeggs 		for (i = 0; i < drm->runl_nr; i++) {
552eb39c613SBen Skeggs 			if (!(args.v.runlists.data & BIT(i)))
553eb39c613SBen Skeggs 				continue;
554eb39c613SBen Skeggs 
555eb39c613SBen Skeggs 			args.v.channels.mthd = NV_DEVICE_HOST_RUNLIST_CHANNELS;
556eb39c613SBen Skeggs 			args.v.channels.data = i;
557eb47db4fSBen Skeggs 
558eb47db4fSBen Skeggs 			ret = nvif_object_mthd(device, NV_DEVICE_V0_INFO, &args, sizeof(args));
559eb47db4fSBen Skeggs 			if (ret || args.v.channels.mthd == NV_DEVICE_INFO_INVALID)
560eb47db4fSBen Skeggs 				return -ENODEV;
561eb47db4fSBen Skeggs 
562eb39c613SBen Skeggs 			drm->runl[i].chan_nr = args.v.channels.data;
563eb39c613SBen Skeggs 			drm->runl[i].chan_id_base = drm->chan_total;
564eb39c613SBen Skeggs 			drm->runl[i].context_base = dma_fence_context_alloc(drm->runl[i].chan_nr);
565eb39c613SBen Skeggs 
566eb39c613SBen Skeggs 			drm->chan_total += drm->runl[i].chan_nr;
567eb39c613SBen Skeggs 		}
568eb39c613SBen Skeggs 	} else {
569eb39c613SBen Skeggs 		drm->runl[0].context_base = dma_fence_context_alloc(drm->chan_nr);
570eb39c613SBen Skeggs 		for (i = 1; i < drm->runl_nr; i++)
571eb39c613SBen Skeggs 			drm->runl[i].context_base = drm->runl[0].context_base;
572eb39c613SBen Skeggs 
573eb39c613SBen Skeggs 	}
574eb39c613SBen Skeggs 
575eb47db4fSBen Skeggs 	return 0;
576eb47db4fSBen Skeggs }
577