chan.c 10.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * Copyright 2012 Red Hat Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Authors: Ben Skeggs
 */
#include "chan.h"

#include <core/client.h>
27
#include <core/gpuobj.h>
28 29
#include <core/oproxy.h>
#include <subdev/mmu.h>
30 31
#include <engine/dma.h>

32 33 34 35 36
struct nvkm_fifo_chan_object {
	struct nvkm_oproxy oproxy;
	struct nvkm_fifo_chan *chan;
	int hash;
};
37

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
static int
nvkm_fifo_chan_child_fini(struct nvkm_oproxy *base, bool suspend)
{
	struct nvkm_fifo_chan_object *object =
		container_of(base, typeof(*object), oproxy);
	struct nvkm_engine *engine  = object->oproxy.object->engine;
	struct nvkm_fifo_chan *chan = object->chan;
	struct nvkm_fifo_engn *engn = &chan->engn[engine->subdev.index];
	const char *name = nvkm_subdev_name[engine->subdev.index];
	int ret = 0;

	if (--engn->usecount)
		return 0;

	if (chan->func->engine_fini) {
		ret = chan->func->engine_fini(chan, engine, suspend);
		if (ret) {
			nvif_error(&chan->object,
				   "detach %s failed, %d\n", name, ret);
			return ret;
		}
	}

	if (engn->object) {
		ret = nvkm_object_fini(engn->object, suspend);
		if (ret && suspend)
			return ret;
	}

	nvif_trace(&chan->object, "detached %s\n", name);
	return ret;
}

static int
nvkm_fifo_chan_child_init(struct nvkm_oproxy *base)
{
	struct nvkm_fifo_chan_object *object =
		container_of(base, typeof(*object), oproxy);
	struct nvkm_engine *engine  = object->oproxy.object->engine;
	struct nvkm_fifo_chan *chan = object->chan;
	struct nvkm_fifo_engn *engn = &chan->engn[engine->subdev.index];
	const char *name = nvkm_subdev_name[engine->subdev.index];
	int ret;

	if (engn->usecount++)
		return 0;

	if (engn->object) {
		ret = nvkm_object_init(engn->object);
		if (ret)
			return ret;
	}

	if (chan->func->engine_init) {
		ret = chan->func->engine_init(chan, engine);
		if (ret) {
			nvif_error(&chan->object,
				   "attach %s failed, %d\n", name, ret);
			return ret;
		}
	}

	nvif_trace(&chan->object, "attached %s\n", name);
	return 0;
}

static void
nvkm_fifo_chan_child_del(struct nvkm_oproxy *base)
{
	struct nvkm_fifo_chan_object *object =
		container_of(base, typeof(*object), oproxy);
	struct nvkm_engine *engine  = object->oproxy.base.engine;
	struct nvkm_fifo_chan *chan = object->chan;
	struct nvkm_fifo_engn *engn = &chan->engn[engine->subdev.index];

	if (chan->func->object_dtor)
		chan->func->object_dtor(chan, object->hash);

	if (!--engn->refcount) {
		if (chan->func->engine_dtor)
			chan->func->engine_dtor(chan, engine);
119
		nvkm_object_del(&engn->object);
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
		if (chan->vm)
			atomic_dec(&chan->vm->engref[engine->subdev.index]);
	}
}

static const struct nvkm_oproxy_func
nvkm_fifo_chan_child_func = {
	.dtor[0] = nvkm_fifo_chan_child_del,
	.init[0] = nvkm_fifo_chan_child_init,
	.fini[0] = nvkm_fifo_chan_child_fini,
};

static int
nvkm_fifo_chan_child_new(const struct nvkm_oclass *oclass, void *data, u32 size,
			 struct nvkm_object **pobject)
{
	struct nvkm_engine *engine = oclass->engine;
	struct nvkm_fifo_chan *chan = nvkm_fifo_chan(oclass->parent);
	struct nvkm_fifo_engn *engn = &chan->engn[engine->subdev.index];
	struct nvkm_fifo_chan_object *object;
	int ret = 0;

	if (!(object = kzalloc(sizeof(*object), GFP_KERNEL)))
		return -ENOMEM;
	nvkm_oproxy_ctor(&nvkm_fifo_chan_child_func, oclass, &object->oproxy);
	object->chan = chan;
	*pobject = &object->oproxy.base;

	if (!engn->refcount++) {
		struct nvkm_oclass cclass = {
			.client = oclass->client,
			.engine = oclass->engine,
		};

		if (chan->vm)
			atomic_inc(&chan->vm->engref[engine->subdev.index]);

		if (engine->func->fifo.cclass) {
			ret = engine->func->fifo.cclass(chan, &cclass,
							&engn->object);
		} else
		if (engine->func->cclass) {
			ret = nvkm_object_new_(engine->func->cclass, &cclass,
					       NULL, 0, &engn->object);
		}
		if (ret)
			return ret;

		if (chan->func->engine_ctor) {
			ret = chan->func->engine_ctor(chan, oclass->engine,
						      engn->object);
			if (ret)
				return ret;
		}
	}

	ret = oclass->base.ctor(&(const struct nvkm_oclass) {
					.base = oclass->base,
					.engn = oclass->engn,
					.handle = oclass->handle,
					.object = oclass->object,
					.client = oclass->client,
					.parent = engn->object ?
						  engn->object :
						  oclass->parent,
					.engine = engine,
				}, data, size, &object->oproxy.object);
	if (ret)
		return ret;

	if (chan->func->object_ctor) {
		object->hash =
			chan->func->object_ctor(chan, object->oproxy.object);
		if (object->hash < 0)
			return object->hash;
	}

	return 0;
}

static int
nvkm_fifo_chan_child_get(struct nvkm_object *object, int index,
			 struct nvkm_oclass *oclass)
{
	struct nvkm_fifo_chan *chan = nvkm_fifo_chan(object);
	struct nvkm_fifo *fifo = chan->fifo;
	struct nvkm_device *device = fifo->engine.subdev.device;
	struct nvkm_engine *engine;
	u64 mask = chan->engines;
	int ret, i, c;

	for (; c = 0, i = __ffs64(mask), mask; mask &= ~(1ULL << i)) {
		if (!(engine = nvkm_device_engine(device, i)))
			continue;
		oclass->engine = engine;
		oclass->base.oclass = 0;

		if (engine->func->fifo.sclass) {
			ret = engine->func->fifo.sclass(oclass, index);
			if (oclass->base.oclass) {
				if (!oclass->base.ctor)
					oclass->base.ctor = nvkm_object_new;
				oclass->ctor = nvkm_fifo_chan_child_new;
				return 0;
			}

			index -= ret;
			continue;
		}

		while (engine->func->sclass[c].oclass) {
			if (c++ == index) {
				oclass->base = engine->func->sclass[index];
				if (!oclass->base.ctor)
					oclass->base.ctor = nvkm_object_new;
				oclass->ctor = nvkm_fifo_chan_child_new;
				return 0;
			}
		}
		index -= c;
	}

242 243 244
	return -EINVAL;
}

245 246 247
static int
nvkm_fifo_chan_ntfy(struct nvkm_object *object, u32 type,
		    struct nvkm_event **pevent)
248
{
249 250 251 252 253 254 255 256 257 258
	struct nvkm_fifo_chan *chan = nvkm_fifo_chan(object);
	if (chan->func->ntfy)
		return chan->func->ntfy(chan, type, pevent);
	return -ENODEV;
}

static int
nvkm_fifo_chan_map(struct nvkm_object *object, u64 *addr, u32 *size)
{
	struct nvkm_fifo_chan *chan = nvkm_fifo_chan(object);
259 260 261 262 263
	*addr = chan->addr;
	*size = chan->size;
	return 0;
}

264 265
static int
nvkm_fifo_chan_rd32(struct nvkm_object *object, u64 addr, u32 *data)
266
{
267
	struct nvkm_fifo_chan *chan = nvkm_fifo_chan(object);
268 269
	if (unlikely(!chan->user)) {
		chan->user = ioremap(chan->addr, chan->size);
270 271
		if (!chan->user)
			return -ENOMEM;
272
	}
273 274 275 276
	if (unlikely(addr + 4 > chan->size))
		return -EINVAL;
	*data = ioread32_native(chan->user + addr);
	return 0;
277 278
}

279 280
static int
nvkm_fifo_chan_wr32(struct nvkm_object *object, u64 addr, u32 data)
281
{
282
	struct nvkm_fifo_chan *chan = nvkm_fifo_chan(object);
283 284
	if (unlikely(!chan->user)) {
		chan->user = ioremap(chan->addr, chan->size);
285 286
		if (!chan->user)
			return -ENOMEM;
287
	}
288 289
	if (unlikely(addr + 4 > chan->size))
		return -EINVAL;
290
	iowrite32_native(data, chan->user + addr);
291 292 293 294 295 296 297 298 299
	return 0;
}

static int
nvkm_fifo_chan_fini(struct nvkm_object *object, bool suspend)
{
	struct nvkm_fifo_chan *chan = nvkm_fifo_chan(object);
	chan->func->fini(chan);
	return 0;
300 301
}

302 303
static int
nvkm_fifo_chan_init(struct nvkm_object *object)
304
{
305 306 307 308 309 310 311 312 313 314 315
	struct nvkm_fifo_chan *chan = nvkm_fifo_chan(object);
	chan->func->init(chan);
	return 0;
}

static void *
nvkm_fifo_chan_dtor(struct nvkm_object *object)
{
	struct nvkm_fifo_chan *chan = nvkm_fifo_chan(object);
	struct nvkm_fifo *fifo = chan->fifo;
	void *data = chan->func->dtor(chan);
316 317
	unsigned long flags;

318 319 320 321 322 323 324
	spin_lock_irqsave(&fifo->lock, flags);
	if (!list_empty(&chan->head)) {
		__clear_bit(chan->chid, fifo->mask);
		list_del(&chan->head);
	}
	spin_unlock_irqrestore(&fifo->lock, flags);

325 326 327
	if (chan->user)
		iounmap(chan->user);

328
	nvkm_vm_ref(NULL, &chan->vm, NULL);
329

330 331 332
	nvkm_gpuobj_del(&chan->push);
	nvkm_gpuobj_del(&chan->inst);
	return data;
333 334
}

335
static const struct nvkm_object_func
336 337 338 339 340 341 342 343 344 345
nvkm_fifo_chan_func = {
	.dtor = nvkm_fifo_chan_dtor,
	.init = nvkm_fifo_chan_init,
	.fini = nvkm_fifo_chan_fini,
	.ntfy = nvkm_fifo_chan_ntfy,
	.map = nvkm_fifo_chan_map,
	.rd32 = nvkm_fifo_chan_rd32,
	.wr32 = nvkm_fifo_chan_wr32,
	.sclass = nvkm_fifo_chan_child_get,
};
346 347

int
348 349 350 351 352
nvkm_fifo_chan_ctor(const struct nvkm_fifo_chan_func *func,
		    struct nvkm_fifo *fifo, u32 size, u32 align, bool zero,
		    u64 vm, u64 push, u64 engines, int bar, u32 base, u32 user,
		    const struct nvkm_oclass *oclass,
		    struct nvkm_fifo_chan *chan)
353
{
354 355 356
	struct nvkm_client *client = oclass->client;
	struct nvkm_device *device = fifo->engine.subdev.device;
	struct nvkm_mmu *mmu = device->mmu;
357 358 359 360
	struct nvkm_dmaobj *dmaobj;
	unsigned long flags;
	int ret;

361 362 363 364 365 366 367 368
	nvkm_object_ctor(&nvkm_fifo_chan_func, oclass, &chan->object);
	chan->func = func;
	chan->fifo = fifo;
	chan->engines = engines;
	INIT_LIST_HEAD(&chan->head);

	/* instance memory */
	ret = nvkm_gpuobj_new(device, size, align, zero, NULL, &chan->inst);
369 370 371
	if (ret)
		return ret;

372 373 374
	/* allocate push buffer ctxdma instance */
	if (push) {
		dmaobj = nvkm_dma_search(device->dma, oclass->client, push);
375 376 377
		if (!dmaobj)
			return -ENOENT;

378 379
		ret = nvkm_object_bind(&dmaobj->object, chan->inst, -16,
				       &chan->push);
380 381 382 383
		if (ret)
			return ret;
	}

384 385 386 387 388 389 390 391
	/* channel address space */
	if (!vm && mmu) {
		if (!client->vm || client->vm->mmu == mmu) {
			ret = nvkm_vm_ref(client->vm, &chan->vm, NULL);
			if (ret)
				return ret;
		} else {
			return -EINVAL;
392
		}
393 394
	} else {
		return -ENOENT;
395 396
	}

397 398 399 400 401
	/* allocate channel id */
	spin_lock_irqsave(&fifo->lock, flags);
	chan->chid = find_first_zero_bit(fifo->mask, NVKM_FIFO_CHID_NR);
	if (chan->chid >= NVKM_FIFO_CHID_NR) {
		spin_unlock_irqrestore(&fifo->lock, flags);
402 403
		return -ENOSPC;
	}
404 405 406
	list_add(&chan->head, &fifo->chan);
	__set_bit(chan->chid, fifo->mask);
	spin_unlock_irqrestore(&fifo->lock, flags);
407

408
	/* determine address of this channel's user registers */
409
	chan->addr = device->func->resource_addr(device, bar) +
410 411 412
		     base + user * chan->chid;
	chan->size = user;

413 414 415
	nvkm_event_send(&fifo->cevent, 1, 0, NULL, 0);
	return 0;
}