162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Copyright 2012 Red Hat Inc.
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
562306a36Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
662306a36Sopenharmony_ci * to deal in the Software without restriction, including without limitation
762306a36Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
862306a36Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
962306a36Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
1062306a36Sopenharmony_ci *
1162306a36Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
1262306a36Sopenharmony_ci * all copies or substantial portions of the Software.
1362306a36Sopenharmony_ci *
1462306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1562306a36Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1662306a36Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1762306a36Sopenharmony_ci * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
1862306a36Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
1962306a36Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2062306a36Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
2162306a36Sopenharmony_ci *
2262306a36Sopenharmony_ci * Authors: Ben Skeggs
2362306a36Sopenharmony_ci */
2462306a36Sopenharmony_ci#include <nvif/push006c.h>
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci#include <nvif/class.h>
2762306a36Sopenharmony_ci#include <nvif/cl0002.h>
2862306a36Sopenharmony_ci#include <nvif/if0020.h>
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci#include "nouveau_drv.h"
3162306a36Sopenharmony_ci#include "nouveau_dma.h"
3262306a36Sopenharmony_ci#include "nouveau_bo.h"
3362306a36Sopenharmony_ci#include "nouveau_chan.h"
3462306a36Sopenharmony_ci#include "nouveau_fence.h"
3562306a36Sopenharmony_ci#include "nouveau_abi16.h"
3662306a36Sopenharmony_ci#include "nouveau_vmm.h"
3762306a36Sopenharmony_ci#include "nouveau_svm.h"
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ciMODULE_PARM_DESC(vram_pushbuf, "Create DMA push buffers in VRAM");
4062306a36Sopenharmony_ciint nouveau_vram_pushbuf;
4162306a36Sopenharmony_cimodule_param_named(vram_pushbuf, nouveau_vram_pushbuf, int, 0400);
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_civoid
4462306a36Sopenharmony_cinouveau_channel_kill(struct nouveau_channel *chan)
4562306a36Sopenharmony_ci{
4662306a36Sopenharmony_ci	atomic_set(&chan->killed, 1);
4762306a36Sopenharmony_ci	if (chan->fence)
4862306a36Sopenharmony_ci		nouveau_fence_context_kill(chan->fence, -ENODEV);
4962306a36Sopenharmony_ci}
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_cistatic int
5262306a36Sopenharmony_cinouveau_channel_killed(struct nvif_event *event, void *repv, u32 repc)
5362306a36Sopenharmony_ci{
5462306a36Sopenharmony_ci	struct nouveau_channel *chan = container_of(event, typeof(*chan), kill);
5562306a36Sopenharmony_ci	struct nouveau_cli *cli = (void *)chan->user.client;
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci	NV_PRINTK(warn, cli, "channel %d killed!\n", chan->chid);
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ci	if (unlikely(!atomic_read(&chan->killed)))
6062306a36Sopenharmony_ci		nouveau_channel_kill(chan);
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci	return NVIF_EVENT_DROP;
6362306a36Sopenharmony_ci}
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ciint
6662306a36Sopenharmony_cinouveau_channel_idle(struct nouveau_channel *chan)
6762306a36Sopenharmony_ci{
6862306a36Sopenharmony_ci	if (likely(chan && chan->fence && !atomic_read(&chan->killed))) {
6962306a36Sopenharmony_ci		struct nouveau_cli *cli = (void *)chan->user.client;
7062306a36Sopenharmony_ci		struct nouveau_fence *fence = NULL;
7162306a36Sopenharmony_ci		int ret;
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_ci		ret = nouveau_fence_new(&fence, chan);
7462306a36Sopenharmony_ci		if (!ret) {
7562306a36Sopenharmony_ci			ret = nouveau_fence_wait(fence, false, false);
7662306a36Sopenharmony_ci			nouveau_fence_unref(&fence);
7762306a36Sopenharmony_ci		}
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci		if (ret) {
8062306a36Sopenharmony_ci			NV_PRINTK(err, cli, "failed to idle channel %d [%s]\n",
8162306a36Sopenharmony_ci				  chan->chid, nvxx_client(&cli->base)->name);
8262306a36Sopenharmony_ci			return ret;
8362306a36Sopenharmony_ci		}
8462306a36Sopenharmony_ci	}
8562306a36Sopenharmony_ci	return 0;
8662306a36Sopenharmony_ci}
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_civoid
8962306a36Sopenharmony_cinouveau_channel_del(struct nouveau_channel **pchan)
9062306a36Sopenharmony_ci{
9162306a36Sopenharmony_ci	struct nouveau_channel *chan = *pchan;
9262306a36Sopenharmony_ci	if (chan) {
9362306a36Sopenharmony_ci		struct nouveau_cli *cli = (void *)chan->user.client;
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ci		if (chan->fence)
9662306a36Sopenharmony_ci			nouveau_fence(chan->drm)->context_del(chan);
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ci		if (cli)
9962306a36Sopenharmony_ci			nouveau_svmm_part(chan->vmm->svmm, chan->inst);
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci		nvif_object_dtor(&chan->blit);
10262306a36Sopenharmony_ci		nvif_object_dtor(&chan->nvsw);
10362306a36Sopenharmony_ci		nvif_object_dtor(&chan->gart);
10462306a36Sopenharmony_ci		nvif_object_dtor(&chan->vram);
10562306a36Sopenharmony_ci		nvif_event_dtor(&chan->kill);
10662306a36Sopenharmony_ci		nvif_object_dtor(&chan->user);
10762306a36Sopenharmony_ci		nvif_mem_dtor(&chan->mem_userd);
10862306a36Sopenharmony_ci		nvif_object_dtor(&chan->push.ctxdma);
10962306a36Sopenharmony_ci		nouveau_vma_del(&chan->push.vma);
11062306a36Sopenharmony_ci		nouveau_bo_unmap(chan->push.buffer);
11162306a36Sopenharmony_ci		if (chan->push.buffer && chan->push.buffer->bo.pin_count)
11262306a36Sopenharmony_ci			nouveau_bo_unpin(chan->push.buffer);
11362306a36Sopenharmony_ci		nouveau_bo_ref(NULL, &chan->push.buffer);
11462306a36Sopenharmony_ci		kfree(chan);
11562306a36Sopenharmony_ci	}
11662306a36Sopenharmony_ci	*pchan = NULL;
11762306a36Sopenharmony_ci}
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_cistatic void
12062306a36Sopenharmony_cinouveau_channel_kick(struct nvif_push *push)
12162306a36Sopenharmony_ci{
12262306a36Sopenharmony_ci	struct nouveau_channel *chan = container_of(push, typeof(*chan), chan._push);
12362306a36Sopenharmony_ci	chan->dma.cur = chan->dma.cur + (chan->chan._push.cur - chan->chan._push.bgn);
12462306a36Sopenharmony_ci	FIRE_RING(chan);
12562306a36Sopenharmony_ci	chan->chan._push.bgn = chan->chan._push.cur;
12662306a36Sopenharmony_ci}
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_cistatic int
12962306a36Sopenharmony_cinouveau_channel_wait(struct nvif_push *push, u32 size)
13062306a36Sopenharmony_ci{
13162306a36Sopenharmony_ci	struct nouveau_channel *chan = container_of(push, typeof(*chan), chan._push);
13262306a36Sopenharmony_ci	int ret;
13362306a36Sopenharmony_ci	chan->dma.cur = chan->dma.cur + (chan->chan._push.cur - chan->chan._push.bgn);
13462306a36Sopenharmony_ci	ret = RING_SPACE(chan, size);
13562306a36Sopenharmony_ci	if (ret == 0) {
13662306a36Sopenharmony_ci		chan->chan._push.bgn = chan->chan._push.mem.object.map.ptr;
13762306a36Sopenharmony_ci		chan->chan._push.bgn = chan->chan._push.bgn + chan->dma.cur;
13862306a36Sopenharmony_ci		chan->chan._push.cur = chan->chan._push.bgn;
13962306a36Sopenharmony_ci		chan->chan._push.end = chan->chan._push.bgn + size;
14062306a36Sopenharmony_ci	}
14162306a36Sopenharmony_ci	return ret;
14262306a36Sopenharmony_ci}
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_cistatic int
14562306a36Sopenharmony_cinouveau_channel_prep(struct nouveau_drm *drm, struct nvif_device *device,
14662306a36Sopenharmony_ci		     u32 size, struct nouveau_channel **pchan)
14762306a36Sopenharmony_ci{
14862306a36Sopenharmony_ci	struct nouveau_cli *cli = (void *)device->object.client;
14962306a36Sopenharmony_ci	struct nv_dma_v0 args = {};
15062306a36Sopenharmony_ci	struct nouveau_channel *chan;
15162306a36Sopenharmony_ci	u32 target;
15262306a36Sopenharmony_ci	int ret;
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_ci	chan = *pchan = kzalloc(sizeof(*chan), GFP_KERNEL);
15562306a36Sopenharmony_ci	if (!chan)
15662306a36Sopenharmony_ci		return -ENOMEM;
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ci	chan->device = device;
15962306a36Sopenharmony_ci	chan->drm = drm;
16062306a36Sopenharmony_ci	chan->vmm = nouveau_cli_vmm(cli);
16162306a36Sopenharmony_ci	atomic_set(&chan->killed, 0);
16262306a36Sopenharmony_ci
16362306a36Sopenharmony_ci	/* allocate memory for dma push buffer */
16462306a36Sopenharmony_ci	target = NOUVEAU_GEM_DOMAIN_GART | NOUVEAU_GEM_DOMAIN_COHERENT;
16562306a36Sopenharmony_ci	if (nouveau_vram_pushbuf)
16662306a36Sopenharmony_ci		target = NOUVEAU_GEM_DOMAIN_VRAM;
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_ci	ret = nouveau_bo_new(cli, size, 0, target, 0, 0, NULL, NULL,
16962306a36Sopenharmony_ci			    &chan->push.buffer);
17062306a36Sopenharmony_ci	if (ret == 0) {
17162306a36Sopenharmony_ci		ret = nouveau_bo_pin(chan->push.buffer, target, false);
17262306a36Sopenharmony_ci		if (ret == 0)
17362306a36Sopenharmony_ci			ret = nouveau_bo_map(chan->push.buffer);
17462306a36Sopenharmony_ci	}
17562306a36Sopenharmony_ci
17662306a36Sopenharmony_ci	if (ret) {
17762306a36Sopenharmony_ci		nouveau_channel_del(pchan);
17862306a36Sopenharmony_ci		return ret;
17962306a36Sopenharmony_ci	}
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_ci	chan->chan._push.mem.object.parent = cli->base.object.parent;
18262306a36Sopenharmony_ci	chan->chan._push.mem.object.client = &cli->base;
18362306a36Sopenharmony_ci	chan->chan._push.mem.object.name = "chanPush";
18462306a36Sopenharmony_ci	chan->chan._push.mem.object.map.ptr = chan->push.buffer->kmap.virtual;
18562306a36Sopenharmony_ci	chan->chan._push.wait = nouveau_channel_wait;
18662306a36Sopenharmony_ci	chan->chan._push.kick = nouveau_channel_kick;
18762306a36Sopenharmony_ci	chan->chan.push = &chan->chan._push;
18862306a36Sopenharmony_ci
18962306a36Sopenharmony_ci	/* create dma object covering the *entire* memory space that the
19062306a36Sopenharmony_ci	 * pushbuf lives in, this is because the GEM code requires that
19162306a36Sopenharmony_ci	 * we be able to call out to other (indirect) push buffers
19262306a36Sopenharmony_ci	 */
19362306a36Sopenharmony_ci	chan->push.addr = chan->push.buffer->offset;
19462306a36Sopenharmony_ci
19562306a36Sopenharmony_ci	if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
19662306a36Sopenharmony_ci		ret = nouveau_vma_new(chan->push.buffer, chan->vmm,
19762306a36Sopenharmony_ci				      &chan->push.vma);
19862306a36Sopenharmony_ci		if (ret) {
19962306a36Sopenharmony_ci			nouveau_channel_del(pchan);
20062306a36Sopenharmony_ci			return ret;
20162306a36Sopenharmony_ci		}
20262306a36Sopenharmony_ci
20362306a36Sopenharmony_ci		chan->push.addr = chan->push.vma->addr;
20462306a36Sopenharmony_ci
20562306a36Sopenharmony_ci		if (device->info.family >= NV_DEVICE_INFO_V0_FERMI)
20662306a36Sopenharmony_ci			return 0;
20762306a36Sopenharmony_ci
20862306a36Sopenharmony_ci		args.target = NV_DMA_V0_TARGET_VM;
20962306a36Sopenharmony_ci		args.access = NV_DMA_V0_ACCESS_VM;
21062306a36Sopenharmony_ci		args.start = 0;
21162306a36Sopenharmony_ci		args.limit = chan->vmm->vmm.limit - 1;
21262306a36Sopenharmony_ci	} else
21362306a36Sopenharmony_ci	if (chan->push.buffer->bo.resource->mem_type == TTM_PL_VRAM) {
21462306a36Sopenharmony_ci		if (device->info.family == NV_DEVICE_INFO_V0_TNT) {
21562306a36Sopenharmony_ci			/* nv04 vram pushbuf hack, retarget to its location in
21662306a36Sopenharmony_ci			 * the framebuffer bar rather than direct vram access..
21762306a36Sopenharmony_ci			 * nfi why this exists, it came from the -nv ddx.
21862306a36Sopenharmony_ci			 */
21962306a36Sopenharmony_ci			args.target = NV_DMA_V0_TARGET_PCI;
22062306a36Sopenharmony_ci			args.access = NV_DMA_V0_ACCESS_RDWR;
22162306a36Sopenharmony_ci			args.start = nvxx_device(device)->func->
22262306a36Sopenharmony_ci				resource_addr(nvxx_device(device), 1);
22362306a36Sopenharmony_ci			args.limit = args.start + device->info.ram_user - 1;
22462306a36Sopenharmony_ci		} else {
22562306a36Sopenharmony_ci			args.target = NV_DMA_V0_TARGET_VRAM;
22662306a36Sopenharmony_ci			args.access = NV_DMA_V0_ACCESS_RDWR;
22762306a36Sopenharmony_ci			args.start = 0;
22862306a36Sopenharmony_ci			args.limit = device->info.ram_user - 1;
22962306a36Sopenharmony_ci		}
23062306a36Sopenharmony_ci	} else {
23162306a36Sopenharmony_ci		if (chan->drm->agp.bridge) {
23262306a36Sopenharmony_ci			args.target = NV_DMA_V0_TARGET_AGP;
23362306a36Sopenharmony_ci			args.access = NV_DMA_V0_ACCESS_RDWR;
23462306a36Sopenharmony_ci			args.start = chan->drm->agp.base;
23562306a36Sopenharmony_ci			args.limit = chan->drm->agp.base +
23662306a36Sopenharmony_ci				     chan->drm->agp.size - 1;
23762306a36Sopenharmony_ci		} else {
23862306a36Sopenharmony_ci			args.target = NV_DMA_V0_TARGET_VM;
23962306a36Sopenharmony_ci			args.access = NV_DMA_V0_ACCESS_RDWR;
24062306a36Sopenharmony_ci			args.start = 0;
24162306a36Sopenharmony_ci			args.limit = chan->vmm->vmm.limit - 1;
24262306a36Sopenharmony_ci		}
24362306a36Sopenharmony_ci	}
24462306a36Sopenharmony_ci
24562306a36Sopenharmony_ci	ret = nvif_object_ctor(&device->object, "abi16PushCtxDma", 0,
24662306a36Sopenharmony_ci			       NV_DMA_FROM_MEMORY, &args, sizeof(args),
24762306a36Sopenharmony_ci			       &chan->push.ctxdma);
24862306a36Sopenharmony_ci	if (ret) {
24962306a36Sopenharmony_ci		nouveau_channel_del(pchan);
25062306a36Sopenharmony_ci		return ret;
25162306a36Sopenharmony_ci	}
25262306a36Sopenharmony_ci
25362306a36Sopenharmony_ci	return 0;
25462306a36Sopenharmony_ci}
25562306a36Sopenharmony_ci
25662306a36Sopenharmony_cistatic int
25762306a36Sopenharmony_cinouveau_channel_ctor(struct nouveau_drm *drm, struct nvif_device *device, bool priv, u64 runm,
25862306a36Sopenharmony_ci		     struct nouveau_channel **pchan)
25962306a36Sopenharmony_ci{
26062306a36Sopenharmony_ci	const struct nvif_mclass hosts[] = {
26162306a36Sopenharmony_ci		{  AMPERE_CHANNEL_GPFIFO_B, 0 },
26262306a36Sopenharmony_ci		{  AMPERE_CHANNEL_GPFIFO_A, 0 },
26362306a36Sopenharmony_ci		{  TURING_CHANNEL_GPFIFO_A, 0 },
26462306a36Sopenharmony_ci		{   VOLTA_CHANNEL_GPFIFO_A, 0 },
26562306a36Sopenharmony_ci		{  PASCAL_CHANNEL_GPFIFO_A, 0 },
26662306a36Sopenharmony_ci		{ MAXWELL_CHANNEL_GPFIFO_A, 0 },
26762306a36Sopenharmony_ci		{  KEPLER_CHANNEL_GPFIFO_B, 0 },
26862306a36Sopenharmony_ci		{  KEPLER_CHANNEL_GPFIFO_A, 0 },
26962306a36Sopenharmony_ci		{   FERMI_CHANNEL_GPFIFO  , 0 },
27062306a36Sopenharmony_ci		{     G82_CHANNEL_GPFIFO  , 0 },
27162306a36Sopenharmony_ci		{    NV50_CHANNEL_GPFIFO  , 0 },
27262306a36Sopenharmony_ci		{    NV40_CHANNEL_DMA     , 0 },
27362306a36Sopenharmony_ci		{    NV17_CHANNEL_DMA     , 0 },
27462306a36Sopenharmony_ci		{    NV10_CHANNEL_DMA     , 0 },
27562306a36Sopenharmony_ci		{    NV03_CHANNEL_DMA     , 0 },
27662306a36Sopenharmony_ci		{}
27762306a36Sopenharmony_ci	};
27862306a36Sopenharmony_ci	struct {
27962306a36Sopenharmony_ci		struct nvif_chan_v0 chan;
28062306a36Sopenharmony_ci		char name[TASK_COMM_LEN+16];
28162306a36Sopenharmony_ci	} args;
28262306a36Sopenharmony_ci	struct nouveau_cli *cli = (void *)device->object.client;
28362306a36Sopenharmony_ci	struct nouveau_channel *chan;
28462306a36Sopenharmony_ci	const u64 plength = 0x10000;
28562306a36Sopenharmony_ci	const u64 ioffset = plength;
28662306a36Sopenharmony_ci	const u64 ilength = 0x02000;
28762306a36Sopenharmony_ci	char name[TASK_COMM_LEN];
28862306a36Sopenharmony_ci	int cid, ret;
28962306a36Sopenharmony_ci	u64 size;
29062306a36Sopenharmony_ci
29162306a36Sopenharmony_ci	cid = nvif_mclass(&device->object, hosts);
29262306a36Sopenharmony_ci	if (cid < 0)
29362306a36Sopenharmony_ci		return cid;
29462306a36Sopenharmony_ci
29562306a36Sopenharmony_ci	if (hosts[cid].oclass < NV50_CHANNEL_GPFIFO)
29662306a36Sopenharmony_ci		size = plength;
29762306a36Sopenharmony_ci	else
29862306a36Sopenharmony_ci		size = ioffset + ilength;
29962306a36Sopenharmony_ci
30062306a36Sopenharmony_ci	/* allocate dma push buffer */
30162306a36Sopenharmony_ci	ret = nouveau_channel_prep(drm, device, size, &chan);
30262306a36Sopenharmony_ci	*pchan = chan;
30362306a36Sopenharmony_ci	if (ret)
30462306a36Sopenharmony_ci		return ret;
30562306a36Sopenharmony_ci
30662306a36Sopenharmony_ci	/* create channel object */
30762306a36Sopenharmony_ci	args.chan.version = 0;
30862306a36Sopenharmony_ci	args.chan.namelen = sizeof(args.name);
30962306a36Sopenharmony_ci	args.chan.runlist = __ffs64(runm);
31062306a36Sopenharmony_ci	args.chan.runq = 0;
31162306a36Sopenharmony_ci	args.chan.priv = priv;
31262306a36Sopenharmony_ci	args.chan.devm = BIT(0);
31362306a36Sopenharmony_ci	if (hosts[cid].oclass < NV50_CHANNEL_GPFIFO) {
31462306a36Sopenharmony_ci		args.chan.vmm = 0;
31562306a36Sopenharmony_ci		args.chan.ctxdma = nvif_handle(&chan->push.ctxdma);
31662306a36Sopenharmony_ci		args.chan.offset = chan->push.addr;
31762306a36Sopenharmony_ci		args.chan.length = 0;
31862306a36Sopenharmony_ci	} else {
31962306a36Sopenharmony_ci		args.chan.vmm = nvif_handle(&chan->vmm->vmm.object);
32062306a36Sopenharmony_ci		if (hosts[cid].oclass < FERMI_CHANNEL_GPFIFO)
32162306a36Sopenharmony_ci			args.chan.ctxdma = nvif_handle(&chan->push.ctxdma);
32262306a36Sopenharmony_ci		else
32362306a36Sopenharmony_ci			args.chan.ctxdma = 0;
32462306a36Sopenharmony_ci		args.chan.offset = ioffset + chan->push.addr;
32562306a36Sopenharmony_ci		args.chan.length = ilength;
32662306a36Sopenharmony_ci	}
32762306a36Sopenharmony_ci	args.chan.huserd = 0;
32862306a36Sopenharmony_ci	args.chan.ouserd = 0;
32962306a36Sopenharmony_ci
33062306a36Sopenharmony_ci	/* allocate userd */
33162306a36Sopenharmony_ci	if (hosts[cid].oclass >= VOLTA_CHANNEL_GPFIFO_A) {
33262306a36Sopenharmony_ci		ret = nvif_mem_ctor(&cli->mmu, "abi16ChanUSERD", NVIF_CLASS_MEM_GF100,
33362306a36Sopenharmony_ci				    NVIF_MEM_VRAM | NVIF_MEM_COHERENT | NVIF_MEM_MAPPABLE,
33462306a36Sopenharmony_ci				    0, PAGE_SIZE, NULL, 0, &chan->mem_userd);
33562306a36Sopenharmony_ci		if (ret)
33662306a36Sopenharmony_ci			return ret;
33762306a36Sopenharmony_ci
33862306a36Sopenharmony_ci		args.chan.huserd = nvif_handle(&chan->mem_userd.object);
33962306a36Sopenharmony_ci		args.chan.ouserd = 0;
34062306a36Sopenharmony_ci
34162306a36Sopenharmony_ci		chan->userd = &chan->mem_userd.object;
34262306a36Sopenharmony_ci	} else {
34362306a36Sopenharmony_ci		chan->userd = &chan->user;
34462306a36Sopenharmony_ci	}
34562306a36Sopenharmony_ci
34662306a36Sopenharmony_ci	get_task_comm(name, current);
34762306a36Sopenharmony_ci	snprintf(args.name, sizeof(args.name), "%s[%d]", name, task_pid_nr(current));
34862306a36Sopenharmony_ci
34962306a36Sopenharmony_ci	ret = nvif_object_ctor(&device->object, "abi16ChanUser", 0, hosts[cid].oclass,
35062306a36Sopenharmony_ci			       &args, sizeof(args), &chan->user);
35162306a36Sopenharmony_ci	if (ret) {
35262306a36Sopenharmony_ci		nouveau_channel_del(pchan);
35362306a36Sopenharmony_ci		return ret;
35462306a36Sopenharmony_ci	}
35562306a36Sopenharmony_ci
35662306a36Sopenharmony_ci	chan->runlist = args.chan.runlist;
35762306a36Sopenharmony_ci	chan->chid = args.chan.chid;
35862306a36Sopenharmony_ci	chan->inst = args.chan.inst;
35962306a36Sopenharmony_ci	chan->token = args.chan.token;
36062306a36Sopenharmony_ci	return 0;
36162306a36Sopenharmony_ci}
36262306a36Sopenharmony_ci
36362306a36Sopenharmony_cistatic int
36462306a36Sopenharmony_cinouveau_channel_init(struct nouveau_channel *chan, u32 vram, u32 gart)
36562306a36Sopenharmony_ci{
36662306a36Sopenharmony_ci	struct nvif_device *device = chan->device;
36762306a36Sopenharmony_ci	struct nouveau_drm *drm = chan->drm;
36862306a36Sopenharmony_ci	struct nv_dma_v0 args = {};
36962306a36Sopenharmony_ci	int ret, i;
37062306a36Sopenharmony_ci
37162306a36Sopenharmony_ci	ret = nvif_object_map(chan->userd, NULL, 0);
37262306a36Sopenharmony_ci	if (ret)
37362306a36Sopenharmony_ci		return ret;
37462306a36Sopenharmony_ci
37562306a36Sopenharmony_ci	if (chan->user.oclass >= FERMI_CHANNEL_GPFIFO) {
37662306a36Sopenharmony_ci		struct {
37762306a36Sopenharmony_ci			struct nvif_event_v0 base;
37862306a36Sopenharmony_ci			struct nvif_chan_event_v0 host;
37962306a36Sopenharmony_ci		} args;
38062306a36Sopenharmony_ci
38162306a36Sopenharmony_ci		args.host.version = 0;
38262306a36Sopenharmony_ci		args.host.type = NVIF_CHAN_EVENT_V0_KILLED;
38362306a36Sopenharmony_ci
38462306a36Sopenharmony_ci		ret = nvif_event_ctor(&chan->user, "abi16ChanKilled", chan->chid,
38562306a36Sopenharmony_ci				      nouveau_channel_killed, false,
38662306a36Sopenharmony_ci				      &args.base, sizeof(args), &chan->kill);
38762306a36Sopenharmony_ci		if (ret == 0)
38862306a36Sopenharmony_ci			ret = nvif_event_allow(&chan->kill);
38962306a36Sopenharmony_ci		if (ret) {
39062306a36Sopenharmony_ci			NV_ERROR(drm, "Failed to request channel kill "
39162306a36Sopenharmony_ci				      "notification: %d\n", ret);
39262306a36Sopenharmony_ci			return ret;
39362306a36Sopenharmony_ci		}
39462306a36Sopenharmony_ci	}
39562306a36Sopenharmony_ci
39662306a36Sopenharmony_ci	/* allocate dma objects to cover all allowed vram, and gart */
39762306a36Sopenharmony_ci	if (device->info.family < NV_DEVICE_INFO_V0_FERMI) {
39862306a36Sopenharmony_ci		if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
39962306a36Sopenharmony_ci			args.target = NV_DMA_V0_TARGET_VM;
40062306a36Sopenharmony_ci			args.access = NV_DMA_V0_ACCESS_VM;
40162306a36Sopenharmony_ci			args.start = 0;
40262306a36Sopenharmony_ci			args.limit = chan->vmm->vmm.limit - 1;
40362306a36Sopenharmony_ci		} else {
40462306a36Sopenharmony_ci			args.target = NV_DMA_V0_TARGET_VRAM;
40562306a36Sopenharmony_ci			args.access = NV_DMA_V0_ACCESS_RDWR;
40662306a36Sopenharmony_ci			args.start = 0;
40762306a36Sopenharmony_ci			args.limit = device->info.ram_user - 1;
40862306a36Sopenharmony_ci		}
40962306a36Sopenharmony_ci
41062306a36Sopenharmony_ci		ret = nvif_object_ctor(&chan->user, "abi16ChanVramCtxDma", vram,
41162306a36Sopenharmony_ci				       NV_DMA_IN_MEMORY, &args, sizeof(args),
41262306a36Sopenharmony_ci				       &chan->vram);
41362306a36Sopenharmony_ci		if (ret)
41462306a36Sopenharmony_ci			return ret;
41562306a36Sopenharmony_ci
41662306a36Sopenharmony_ci		if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
41762306a36Sopenharmony_ci			args.target = NV_DMA_V0_TARGET_VM;
41862306a36Sopenharmony_ci			args.access = NV_DMA_V0_ACCESS_VM;
41962306a36Sopenharmony_ci			args.start = 0;
42062306a36Sopenharmony_ci			args.limit = chan->vmm->vmm.limit - 1;
42162306a36Sopenharmony_ci		} else
42262306a36Sopenharmony_ci		if (chan->drm->agp.bridge) {
42362306a36Sopenharmony_ci			args.target = NV_DMA_V0_TARGET_AGP;
42462306a36Sopenharmony_ci			args.access = NV_DMA_V0_ACCESS_RDWR;
42562306a36Sopenharmony_ci			args.start = chan->drm->agp.base;
42662306a36Sopenharmony_ci			args.limit = chan->drm->agp.base +
42762306a36Sopenharmony_ci				     chan->drm->agp.size - 1;
42862306a36Sopenharmony_ci		} else {
42962306a36Sopenharmony_ci			args.target = NV_DMA_V0_TARGET_VM;
43062306a36Sopenharmony_ci			args.access = NV_DMA_V0_ACCESS_RDWR;
43162306a36Sopenharmony_ci			args.start = 0;
43262306a36Sopenharmony_ci			args.limit = chan->vmm->vmm.limit - 1;
43362306a36Sopenharmony_ci		}
43462306a36Sopenharmony_ci
43562306a36Sopenharmony_ci		ret = nvif_object_ctor(&chan->user, "abi16ChanGartCtxDma", gart,
43662306a36Sopenharmony_ci				       NV_DMA_IN_MEMORY, &args, sizeof(args),
43762306a36Sopenharmony_ci				       &chan->gart);
43862306a36Sopenharmony_ci		if (ret)
43962306a36Sopenharmony_ci			return ret;
44062306a36Sopenharmony_ci	}
44162306a36Sopenharmony_ci
44262306a36Sopenharmony_ci	/* initialise dma tracking parameters */
44362306a36Sopenharmony_ci	switch (chan->user.oclass) {
44462306a36Sopenharmony_ci	case NV03_CHANNEL_DMA:
44562306a36Sopenharmony_ci	case NV10_CHANNEL_DMA:
44662306a36Sopenharmony_ci	case NV17_CHANNEL_DMA:
44762306a36Sopenharmony_ci	case NV40_CHANNEL_DMA:
44862306a36Sopenharmony_ci		chan->user_put = 0x40;
44962306a36Sopenharmony_ci		chan->user_get = 0x44;
45062306a36Sopenharmony_ci		chan->dma.max = (0x10000 / 4) - 2;
45162306a36Sopenharmony_ci		break;
45262306a36Sopenharmony_ci	default:
45362306a36Sopenharmony_ci		chan->user_put = 0x40;
45462306a36Sopenharmony_ci		chan->user_get = 0x44;
45562306a36Sopenharmony_ci		chan->user_get_hi = 0x60;
45662306a36Sopenharmony_ci		chan->dma.ib_base =  0x10000 / 4;
45762306a36Sopenharmony_ci		chan->dma.ib_max  = NV50_DMA_IB_MAX;
45862306a36Sopenharmony_ci		chan->dma.ib_put  = 0;
45962306a36Sopenharmony_ci		chan->dma.ib_free = chan->dma.ib_max - chan->dma.ib_put;
46062306a36Sopenharmony_ci		chan->dma.max = chan->dma.ib_base;
46162306a36Sopenharmony_ci		break;
46262306a36Sopenharmony_ci	}
46362306a36Sopenharmony_ci
46462306a36Sopenharmony_ci	chan->dma.put = 0;
46562306a36Sopenharmony_ci	chan->dma.cur = chan->dma.put;
46662306a36Sopenharmony_ci	chan->dma.free = chan->dma.max - chan->dma.cur;
46762306a36Sopenharmony_ci
46862306a36Sopenharmony_ci	ret = PUSH_WAIT(chan->chan.push, NOUVEAU_DMA_SKIPS);
46962306a36Sopenharmony_ci	if (ret)
47062306a36Sopenharmony_ci		return ret;
47162306a36Sopenharmony_ci
47262306a36Sopenharmony_ci	for (i = 0; i < NOUVEAU_DMA_SKIPS; i++)
47362306a36Sopenharmony_ci		PUSH_DATA(chan->chan.push, 0x00000000);
47462306a36Sopenharmony_ci
47562306a36Sopenharmony_ci	/* allocate software object class (used for fences on <= nv05) */
47662306a36Sopenharmony_ci	if (device->info.family < NV_DEVICE_INFO_V0_CELSIUS) {
47762306a36Sopenharmony_ci		ret = nvif_object_ctor(&chan->user, "abi16NvswFence", 0x006e,
47862306a36Sopenharmony_ci				       NVIF_CLASS_SW_NV04,
47962306a36Sopenharmony_ci				       NULL, 0, &chan->nvsw);
48062306a36Sopenharmony_ci		if (ret)
48162306a36Sopenharmony_ci			return ret;
48262306a36Sopenharmony_ci
48362306a36Sopenharmony_ci		ret = PUSH_WAIT(chan->chan.push, 2);
48462306a36Sopenharmony_ci		if (ret)
48562306a36Sopenharmony_ci			return ret;
48662306a36Sopenharmony_ci
48762306a36Sopenharmony_ci		PUSH_NVSQ(chan->chan.push, NV_SW, 0x0000, chan->nvsw.handle);
48862306a36Sopenharmony_ci		PUSH_KICK(chan->chan.push);
48962306a36Sopenharmony_ci	}
49062306a36Sopenharmony_ci
49162306a36Sopenharmony_ci	/* initialise synchronisation */
49262306a36Sopenharmony_ci	return nouveau_fence(chan->drm)->context_new(chan);
49362306a36Sopenharmony_ci}
49462306a36Sopenharmony_ci
49562306a36Sopenharmony_ciint
49662306a36Sopenharmony_cinouveau_channel_new(struct nouveau_drm *drm, struct nvif_device *device,
49762306a36Sopenharmony_ci		    bool priv, u64 runm, u32 vram, u32 gart, struct nouveau_channel **pchan)
49862306a36Sopenharmony_ci{
49962306a36Sopenharmony_ci	struct nouveau_cli *cli = (void *)device->object.client;
50062306a36Sopenharmony_ci	int ret;
50162306a36Sopenharmony_ci
50262306a36Sopenharmony_ci	ret = nouveau_channel_ctor(drm, device, priv, runm, pchan);
50362306a36Sopenharmony_ci	if (ret) {
50462306a36Sopenharmony_ci		NV_PRINTK(dbg, cli, "channel create, %d\n", ret);
50562306a36Sopenharmony_ci		return ret;
50662306a36Sopenharmony_ci	}
50762306a36Sopenharmony_ci
50862306a36Sopenharmony_ci	ret = nouveau_channel_init(*pchan, vram, gart);
50962306a36Sopenharmony_ci	if (ret) {
51062306a36Sopenharmony_ci		NV_PRINTK(err, cli, "channel failed to initialise, %d\n", ret);
51162306a36Sopenharmony_ci		nouveau_channel_del(pchan);
51262306a36Sopenharmony_ci		return ret;
51362306a36Sopenharmony_ci	}
51462306a36Sopenharmony_ci
51562306a36Sopenharmony_ci	ret = nouveau_svmm_join((*pchan)->vmm->svmm, (*pchan)->inst);
51662306a36Sopenharmony_ci	if (ret)
51762306a36Sopenharmony_ci		nouveau_channel_del(pchan);
51862306a36Sopenharmony_ci
51962306a36Sopenharmony_ci	return ret;
52062306a36Sopenharmony_ci}
52162306a36Sopenharmony_ci
52262306a36Sopenharmony_civoid
52362306a36Sopenharmony_cinouveau_channels_fini(struct nouveau_drm *drm)
52462306a36Sopenharmony_ci{
52562306a36Sopenharmony_ci	kfree(drm->runl);
52662306a36Sopenharmony_ci}
52762306a36Sopenharmony_ci
52862306a36Sopenharmony_ciint
52962306a36Sopenharmony_cinouveau_channels_init(struct nouveau_drm *drm)
53062306a36Sopenharmony_ci{
53162306a36Sopenharmony_ci	struct {
53262306a36Sopenharmony_ci		struct nv_device_info_v1 m;
53362306a36Sopenharmony_ci		struct {
53462306a36Sopenharmony_ci			struct nv_device_info_v1_data channels;
53562306a36Sopenharmony_ci			struct nv_device_info_v1_data runlists;
53662306a36Sopenharmony_ci		} v;
53762306a36Sopenharmony_ci	} args = {
53862306a36Sopenharmony_ci		.m.version = 1,
53962306a36Sopenharmony_ci		.m.count = sizeof(args.v) / sizeof(args.v.channels),
54062306a36Sopenharmony_ci		.v.channels.mthd = NV_DEVICE_HOST_CHANNELS,
54162306a36Sopenharmony_ci		.v.runlists.mthd = NV_DEVICE_HOST_RUNLISTS,
54262306a36Sopenharmony_ci	};
54362306a36Sopenharmony_ci	struct nvif_object *device = &drm->client.device.object;
54462306a36Sopenharmony_ci	int ret, i;
54562306a36Sopenharmony_ci
54662306a36Sopenharmony_ci	ret = nvif_object_mthd(device, NV_DEVICE_V0_INFO, &args, sizeof(args));
54762306a36Sopenharmony_ci	if (ret ||
54862306a36Sopenharmony_ci	    args.v.runlists.mthd == NV_DEVICE_INFO_INVALID || !args.v.runlists.data ||
54962306a36Sopenharmony_ci	    args.v.channels.mthd == NV_DEVICE_INFO_INVALID)
55062306a36Sopenharmony_ci		return -ENODEV;
55162306a36Sopenharmony_ci
55262306a36Sopenharmony_ci	drm->chan_nr = drm->chan_total = args.v.channels.data;
55362306a36Sopenharmony_ci	drm->runl_nr = fls64(args.v.runlists.data);
55462306a36Sopenharmony_ci	drm->runl = kcalloc(drm->runl_nr, sizeof(*drm->runl), GFP_KERNEL);
55562306a36Sopenharmony_ci	if (!drm->runl)
55662306a36Sopenharmony_ci		return -ENOMEM;
55762306a36Sopenharmony_ci
55862306a36Sopenharmony_ci	if (drm->chan_nr == 0) {
55962306a36Sopenharmony_ci		for (i = 0; i < drm->runl_nr; i++) {
56062306a36Sopenharmony_ci			if (!(args.v.runlists.data & BIT(i)))
56162306a36Sopenharmony_ci				continue;
56262306a36Sopenharmony_ci
56362306a36Sopenharmony_ci			args.v.channels.mthd = NV_DEVICE_HOST_RUNLIST_CHANNELS;
56462306a36Sopenharmony_ci			args.v.channels.data = i;
56562306a36Sopenharmony_ci
56662306a36Sopenharmony_ci			ret = nvif_object_mthd(device, NV_DEVICE_V0_INFO, &args, sizeof(args));
56762306a36Sopenharmony_ci			if (ret || args.v.channels.mthd == NV_DEVICE_INFO_INVALID)
56862306a36Sopenharmony_ci				return -ENODEV;
56962306a36Sopenharmony_ci
57062306a36Sopenharmony_ci			drm->runl[i].chan_nr = args.v.channels.data;
57162306a36Sopenharmony_ci			drm->runl[i].chan_id_base = drm->chan_total;
57262306a36Sopenharmony_ci			drm->runl[i].context_base = dma_fence_context_alloc(drm->runl[i].chan_nr);
57362306a36Sopenharmony_ci
57462306a36Sopenharmony_ci			drm->chan_total += drm->runl[i].chan_nr;
57562306a36Sopenharmony_ci		}
57662306a36Sopenharmony_ci	} else {
57762306a36Sopenharmony_ci		drm->runl[0].context_base = dma_fence_context_alloc(drm->chan_nr);
57862306a36Sopenharmony_ci		for (i = 1; i < drm->runl_nr; i++)
57962306a36Sopenharmony_ci			drm->runl[i].context_base = drm->runl[0].context_base;
58062306a36Sopenharmony_ci
58162306a36Sopenharmony_ci	}
58262306a36Sopenharmony_ci
58362306a36Sopenharmony_ci	return 0;
58462306a36Sopenharmony_ci}
585