nouveau_abi16.c 13.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
/*
 * 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.
 *
 */

24 25 26 27 28
#include <core/object.h>
#include <core/client.h>
#include <core/device.h>
#include <core/class.h>
#include <core/mm.h>
29

30 31 32
#include <subdev/fb.h>
#include <subdev/timer.h>
#include <subdev/instmem.h>
33
#include <engine/graph.h>
34 35

#include "nouveau_drm.h"
36
#include "nouveau_dma.h"
37 38
#include "nouveau_gem.h"
#include "nouveau_chan.h"
39
#include "nouveau_abi16.h"
40

41 42
void nouveau_drm_hack_device(struct nouveau_drm *, struct nvif_device *);

43 44 45 46
struct nouveau_abi16 *
nouveau_abi16_get(struct drm_file *file_priv, struct drm_device *dev)
{
	struct nouveau_cli *cli = nouveau_cli(file_priv);
47
	struct nouveau_drm *drm = nouveau_drm(dev);
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
	mutex_lock(&cli->mutex);
	if (!cli->abi16) {
		struct nouveau_abi16 *abi16;
		cli->abi16 = abi16 = kzalloc(sizeof(*abi16), GFP_KERNEL);
		if (cli->abi16) {
			INIT_LIST_HEAD(&abi16->channels);
			abi16->client = nv_object(cli);

			/* allocate device object targeting client's default
			 * device (ie. the one that belongs to the fd it
			 * opened)
			 */
			if (nouveau_object_new(abi16->client, NVDRM_CLIENT,
					       NVDRM_DEVICE, 0x0080,
					       &(struct nv_device_class) {
						.device = ~0ULL,
					       },
					       sizeof(struct nv_device_class),
66 67 68
					       (struct nouveau_object **)
					       &abi16->device.object) == 0) {
				nouveau_drm_hack_device(drm, &abi16->device);
69
				return cli->abi16;
70
			}
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

			kfree(cli->abi16);
			cli->abi16 = NULL;
		}

		mutex_unlock(&cli->mutex);
	}
	return cli->abi16;
}

int
nouveau_abi16_put(struct nouveau_abi16 *abi16, int ret)
{
	struct nouveau_cli *cli = (void *)abi16->client;
	mutex_unlock(&cli->mutex);
	return ret;
}

u16
nouveau_abi16_swclass(struct nouveau_drm *drm)
{
92 93
	switch (drm->device.info.family) {
	case NV_DEVICE_INFO_V0_TNT:
94
		return 0x006e;
95 96 97 98
	case NV_DEVICE_INFO_V0_CELSIUS:
	case NV_DEVICE_INFO_V0_KELVIN:
	case NV_DEVICE_INFO_V0_RANKINE:
	case NV_DEVICE_INFO_V0_CURIE:
99
		return 0x016e;
100
	case NV_DEVICE_INFO_V0_TESLA:
101
		return 0x506e;
102 103 104
	case NV_DEVICE_INFO_V0_FERMI:
	case NV_DEVICE_INFO_V0_KEPLER:
	case NV_DEVICE_INFO_V0_MAXWELL:
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
		return 0x906e;
	}

	return 0x0000;
}

static void
nouveau_abi16_ntfy_fini(struct nouveau_abi16_chan *chan,
			struct nouveau_abi16_ntfy *ntfy)
{
	nouveau_mm_free(&chan->heap, &ntfy->node);
	list_del(&ntfy->head);
	kfree(ntfy);
}

static void
nouveau_abi16_chan_fini(struct nouveau_abi16 *abi16,
			struct nouveau_abi16_chan *chan)
{
	struct nouveau_abi16_ntfy *ntfy, *temp;

126 127 128 129 130
	/* wait for all activity to stop before releasing notify object, which
	 * may be still in use */
	if (chan->chan && chan->ntfy)
		nouveau_channel_idle(chan->chan);

131 132 133 134 135 136 137
	/* cleanup notifier state */
	list_for_each_entry_safe(ntfy, temp, &chan->notifiers, head) {
		nouveau_abi16_ntfy_fini(chan, ntfy);
	}

	if (chan->ntfy) {
		nouveau_bo_vma_del(chan->ntfy, &chan->ntfy_vma);
138
		nouveau_bo_unpin(chan->ntfy);
139
		drm_gem_object_unreference_unlocked(&chan->ntfy->gem);
140 141 142 143 144 145 146
	}

	if (chan->heap.block_size)
		nouveau_mm_fini(&chan->heap);

	/* destroy channel object, all children will be killed too */
	if (chan->chan) {
147
		abi16->handles &= ~(1ULL << (chan->chan->handle & 0xffff));
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
		nouveau_channel_del(&chan->chan);
	}

	list_del(&chan->head);
	kfree(chan);
}

void
nouveau_abi16_fini(struct nouveau_abi16 *abi16)
{
	struct nouveau_cli *cli = (void *)abi16->client;
	struct nouveau_abi16_chan *chan, *temp;

	/* cleanup channels */
	list_for_each_entry_safe(chan, temp, &abi16->channels, head) {
		nouveau_abi16_chan_fini(abi16, chan);
	}

	/* destroy the device object */
	nouveau_object_del(abi16->client, NVDRM_CLIENT, NVDRM_DEVICE);

	kfree(cli->abi16);
	cli->abi16 = NULL;
}
172 173 174 175

int
nouveau_abi16_ioctl_getparam(ABI16_IOCTL_ARGS)
{
176
	struct nouveau_cli *cli = nouveau_cli(file_priv);
177
	struct nouveau_drm *drm = nouveau_drm(dev);
178 179 180
	struct nvif_device *device = &drm->device;
	struct nouveau_timer *ptimer = nvkm_timer(device);
	struct nouveau_graph *graph = nvkm_gr(device);
181 182 183 184
	struct drm_nouveau_getparam *getparam = data;

	switch (getparam->param) {
	case NOUVEAU_GETPARAM_CHIPSET_ID:
185
		getparam->value = device->info.chipset;
186 187
		break;
	case NOUVEAU_GETPARAM_PCI_VENDOR:
188
		if (nv_device_is_pci(nvkm_device(device)))
A
Alexandre Courbot 已提交
189 190 191
			getparam->value = dev->pdev->vendor;
		else
			getparam->value = 0;
192 193
		break;
	case NOUVEAU_GETPARAM_PCI_DEVICE:
194
		if (nv_device_is_pci(nvkm_device(device)))
A
Alexandre Courbot 已提交
195 196 197
			getparam->value = dev->pdev->device;
		else
			getparam->value = 0;
198 199
		break;
	case NOUVEAU_GETPARAM_BUS_TYPE:
200
		if (!nv_device_is_pci(nvkm_device(device)))
A
Alexandre Courbot 已提交
201 202
			getparam->value = 3;
		else
203 204 205 206 207 208 209 210 211
		if (drm_pci_device_is_agp(dev))
			getparam->value = 0;
		else
		if (!pci_is_pcie(dev->pdev))
			getparam->value = 1;
		else
			getparam->value = 2;
		break;
	case NOUVEAU_GETPARAM_FB_SIZE:
212
		getparam->value = drm->gem.vram_available;
213 214
		break;
	case NOUVEAU_GETPARAM_AGP_SIZE:
215
		getparam->value = drm->gem.gart_available;
216 217 218 219 220
		break;
	case NOUVEAU_GETPARAM_VM_VRAM_BASE:
		getparam->value = 0; /* deprecated */
		break;
	case NOUVEAU_GETPARAM_PTIMER_TIME:
221
		getparam->value = ptimer->read(ptimer);
222 223 224 225 226 227 228 229
		break;
	case NOUVEAU_GETPARAM_HAS_BO_USAGE:
		getparam->value = 1;
		break;
	case NOUVEAU_GETPARAM_HAS_PAGEFLIP:
		getparam->value = 1;
		break;
	case NOUVEAU_GETPARAM_GRAPH_UNITS:
230 231
		getparam->value = graph->units ? graph->units(graph) : 0;
		break;
232
	default:
233
		NV_PRINTK(debug, cli, "unknown parameter %lld\n", getparam->param);
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
		return -EINVAL;
	}

	return 0;
}

int
nouveau_abi16_ioctl_setparam(ABI16_IOCTL_ARGS)
{
	return -EINVAL;
}

int
nouveau_abi16_ioctl_channel_alloc(ABI16_IOCTL_ARGS)
{
	struct drm_nouveau_channel_alloc *init = data;
250 251 252 253 254
	struct nouveau_cli *cli = nouveau_cli(file_priv);
	struct nouveau_drm *drm = nouveau_drm(dev);
	struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
	struct nouveau_abi16_chan *chan;
	struct nouveau_client *client;
255
	struct nvif_device *device;
256 257
	struct nouveau_instmem *imem;
	struct nouveau_fb *pfb;
258 259
	int ret;

260 261
	if (unlikely(!abi16))
		return -ENOMEM;
262 263 264 265

	if (!drm->channel)
		return nouveau_abi16_put(abi16, -ENODEV);

266
	client = nv_client(abi16->client);
267 268 269
	device = &abi16->device;
	imem   = nvkm_instmem(device);
	pfb    = nvkm_fb(device);
270

271
	/* hack to allow channel engine type specification on kepler */
272
	if (device->info.family >= NV_DEVICE_INFO_V0_KEPLER) {
273 274 275 276 277 278 279 280 281 282 283 284 285 286
		if (init->fb_ctxdma_handle != ~0)
			init->fb_ctxdma_handle = NVE0_CHANNEL_IND_ENGINE_GR;
		else
			init->fb_ctxdma_handle = init->tt_ctxdma_handle;

		/* allow flips to be executed if this is a graphics channel */
		init->tt_ctxdma_handle = 0;
		if (init->fb_ctxdma_handle == NVE0_CHANNEL_IND_ENGINE_GR)
			init->tt_ctxdma_handle = 1;
	}

	if (init->fb_ctxdma_handle == ~0 || init->tt_ctxdma_handle == ~0)
		return nouveau_abi16_put(abi16, -EINVAL);

287
	/* allocate "abi16 channel" data and make up a handle for it */
288 289
	init->channel = __ffs64(~abi16->handles);
	if (~abi16->handles == 0)
290 291 292 293 294 295 296 297
		return nouveau_abi16_put(abi16, -ENOSPC);

	chan = kzalloc(sizeof(*chan), GFP_KERNEL);
	if (!chan)
		return nouveau_abi16_put(abi16, -ENOMEM);

	INIT_LIST_HEAD(&chan->notifiers);
	list_add(&chan->head, &abi16->channels);
298
	abi16->handles |= (1ULL << init->channel);
299

300 301 302 303
	/* create channel object and initialise dma and fence management */
	ret = nouveau_channel_new(drm, cli, NVDRM_DEVICE, NVDRM_CHAN |
				  init->channel, init->fb_ctxdma_handle,
				  init->tt_ctxdma_handle, &chan->chan);
304
	if (ret)
305 306
		goto done;

307
	if (device->info.family >= NV_DEVICE_INFO_V0_TESLA)
308 309 310 311
		init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_VRAM |
					NOUVEAU_GEM_DOMAIN_GART;
	else
	if (chan->chan->push.buffer->bo.mem.mem_type == TTM_PL_VRAM)
312
		init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_VRAM;
313 314
	else
		init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_GART;
315

316
	if (device->info.family < NV_DEVICE_INFO_V0_CELSIUS) {
317 318 319
		init->subchan[0].handle = 0x00000000;
		init->subchan[0].grclass = 0x0000;
		init->subchan[1].handle = NvSw;
320
		init->subchan[1].grclass = 0x506e;
321 322 323 324
		init->nr_subchan = 2;
	}

	/* Named memory object area */
325 326 327 328 329 330 331
	ret = nouveau_gem_new(dev, PAGE_SIZE, 0, NOUVEAU_GEM_DOMAIN_GART,
			      0, 0, &chan->ntfy);
	if (ret == 0)
		ret = nouveau_bo_pin(chan->ntfy, TTM_PL_FLAG_TT);
	if (ret)
		goto done;

332
	if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
333 334 335 336 337 338
		ret = nouveau_bo_vma_add(chan->ntfy, client->vm,
					&chan->ntfy_vma);
		if (ret)
			goto done;
	}

339
	ret = drm_gem_handle_create(file_priv, &chan->ntfy->gem,
340
				    &init->notifier_handle);
341 342
	if (ret)
		goto done;
343

344 345 346 347 348
	ret = nouveau_mm_init(&chan->heap, 0, PAGE_SIZE, 1);
done:
	if (ret)
		nouveau_abi16_chan_fini(abi16, chan);
	return nouveau_abi16_put(abi16, ret);
349 350
}

351

352 353 354 355
int
nouveau_abi16_ioctl_channel_free(ABI16_IOCTL_ARGS)
{
	struct drm_nouveau_channel_free *req = data;
356 357 358
	struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
	struct nouveau_abi16_chan *chan;
	int ret = -ENOENT;
359

360 361
	if (unlikely(!abi16))
		return -ENOMEM;
362

363 364 365 366 367 368 369 370
	list_for_each_entry(chan, &abi16->channels, head) {
		if (chan->chan->handle == (NVDRM_CHAN | req->channel)) {
			nouveau_abi16_chan_fini(abi16, chan);
			return nouveau_abi16_put(abi16, 0);
		}
	}

	return nouveau_abi16_put(abi16, ret);
371 372 373 374 375 376
}

int
nouveau_abi16_ioctl_grobj_alloc(ABI16_IOCTL_ARGS)
{
	struct drm_nouveau_grobj_alloc *init = data;
377 378 379
	struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
	struct nouveau_drm *drm = nouveau_drm(dev);
	struct nouveau_object *object;
380 381
	int ret;

382 383 384
	if (unlikely(!abi16))
		return -ENOMEM;

385
	if (init->handle == ~0)
386
		return nouveau_abi16_put(abi16, -EINVAL);
387 388 389

	/* compatibility with userspace that assumes 506e for all chipsets */
	if (init->class == 0x506e) {
390
		init->class = nouveau_abi16_swclass(drm);
391
		if (init->class == 0x906e)
392
			return nouveau_abi16_put(abi16, 0);
393 394
	}

395 396 397
	ret = nouveau_object_new(abi16->client, NVDRM_CHAN | init->channel,
				  init->handle, init->class, NULL, 0, &object);
	return nouveau_abi16_put(abi16, ret);
398 399 400 401 402
}

int
nouveau_abi16_ioctl_notifierobj_alloc(ABI16_IOCTL_ARGS)
{
403 404 405
	struct drm_nouveau_notifierobj_alloc *info = data;
	struct nouveau_drm *drm = nouveau_drm(dev);
	struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
406
	struct nouveau_abi16_chan *chan = NULL, *temp;
407
	struct nouveau_abi16_ntfy *ntfy;
408
	struct nvif_device *device = &abi16->device;
409
	struct nouveau_object *object;
410
	struct nv_dma_class args = {};
411 412
	int ret;

413 414 415
	if (unlikely(!abi16))
		return -ENOMEM;

416
	/* completely unnecessary for these chipsets... */
417
	if (unlikely(device->info.family >= NV_DEVICE_INFO_V0_FERMI))
418
		return nouveau_abi16_put(abi16, -EINVAL);
419

420 421 422
	list_for_each_entry(temp, &abi16->channels, head) {
		if (temp->chan->handle == (NVDRM_CHAN | info->channel)) {
			chan = temp;
423
			break;
424
		}
425
	}
426

427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
	if (!chan)
		return nouveau_abi16_put(abi16, -ENOENT);

	ntfy = kzalloc(sizeof(*ntfy), GFP_KERNEL);
	if (!ntfy)
		return nouveau_abi16_put(abi16, -ENOMEM);

	list_add(&ntfy->head, &chan->notifiers);
	ntfy->handle = info->handle;

	ret = nouveau_mm_head(&chan->heap, 1, info->size, info->size, 1,
			      &ntfy->node);
	if (ret)
		goto done;

	args.start = ntfy->node->offset;
	args.limit = ntfy->node->offset + ntfy->node->length - 1;
444
	if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
		args.flags  = NV_DMA_TARGET_VM | NV_DMA_ACCESS_VM;
		args.start += chan->ntfy_vma.offset;
		args.limit += chan->ntfy_vma.offset;
	} else
	if (drm->agp.stat == ENABLED) {
		args.flags  = NV_DMA_TARGET_AGP | NV_DMA_ACCESS_RDWR;
		args.start += drm->agp.base + chan->ntfy->bo.offset;
		args.limit += drm->agp.base + chan->ntfy->bo.offset;
	} else {
		args.flags  = NV_DMA_TARGET_VM | NV_DMA_ACCESS_RDWR;
		args.start += chan->ntfy->bo.offset;
		args.limit += chan->ntfy->bo.offset;
	}

	ret = nouveau_object_new(abi16->client, chan->chan->handle,
				 ntfy->handle, 0x003d, &args,
				 sizeof(args), &object);
	if (ret)
		goto done;

465 466
	info->offset = ntfy->node->offset;

467 468 469 470
done:
	if (ret)
		nouveau_abi16_ntfy_fini(chan, ntfy);
	return nouveau_abi16_put(abi16, ret);
471 472 473 474 475
}

int
nouveau_abi16_ioctl_gpuobj_free(ABI16_IOCTL_ARGS)
{
476 477
	struct drm_nouveau_gpuobj_free *fini = data;
	struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
478
	struct nouveau_abi16_chan *chan = NULL, *temp;
479
	struct nouveau_abi16_ntfy *ntfy;
480 481
	int ret;

482 483 484
	if (unlikely(!abi16))
		return -ENOMEM;

485 486 487
	list_for_each_entry(temp, &abi16->channels, head) {
		if (temp->chan->handle == (NVDRM_CHAN | fini->channel)) {
			chan = temp;
488
			break;
489
		}
490 491 492 493
	}

	if (!chan)
		return nouveau_abi16_put(abi16, -ENOENT);
494

495 496
	/* synchronize with the user channel and destroy the gpu object */
	nouveau_channel_idle(chan->chan);
497

498 499 500 501 502 503 504 505 506 507 508 509 510 511
	ret = nouveau_object_del(abi16->client, chan->chan->handle, fini->handle);
	if (ret)
		return nouveau_abi16_put(abi16, ret);

	/* cleanup extra state if this object was a notifier */
	list_for_each_entry(ntfy, &chan->notifiers, head) {
		if (ntfy->handle == fini->handle) {
			nouveau_mm_free(&chan->heap, &ntfy->node);
			list_del(&ntfy->head);
			break;
		}
	}

	return nouveau_abi16_put(abi16, 0);
512
}