nouveau_abi16.c 14.7 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
#include <nvif/client.h>
#include <nvif/driver.h>
#include <nvif/ioctl.h>
27
#include <nvif/class.h>
28

29
#include "nouveau_drm.h"
30
#include "nouveau_dma.h"
31 32
#include "nouveau_gem.h"
#include "nouveau_chan.h"
33
#include "nouveau_abi16.h"
34 35 36 37 38 39 40 41 42 43

struct nouveau_abi16 *
nouveau_abi16_get(struct drm_file *file_priv, struct drm_device *dev)
{
	struct nouveau_cli *cli = nouveau_cli(file_priv);
	mutex_lock(&cli->mutex);
	if (!cli->abi16) {
		struct nouveau_abi16 *abi16;
		cli->abi16 = abi16 = kzalloc(sizeof(*abi16), GFP_KERNEL);
		if (cli->abi16) {
44 45 46 47
			struct nv_device_v0 args = {
				.device = ~0ULL,
			};

48 49 50 51 52 53
			INIT_LIST_HEAD(&abi16->channels);

			/* allocate device object targeting client's default
			 * device (ie. the one that belongs to the fd it
			 * opened)
			 */
54
			if (nvif_device_init(&cli->base.object,
55
					     NOUVEAU_ABI16_DEVICE, NV_DEVICE,
56
					     &args, sizeof(args),
57
					     &abi16->device) == 0)
58 59 60 61 62 63 64 65 66 67 68 69 70 71
				return cli->abi16;

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

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

int
nouveau_abi16_put(struct nouveau_abi16 *abi16, int ret)
{
72
	struct nouveau_cli *cli = (void *)abi16->device.object.client;
73 74 75 76
	mutex_unlock(&cli->mutex);
	return ret;
}

77
s32
78 79
nouveau_abi16_swclass(struct nouveau_drm *drm)
{
80 81
	switch (drm->device.info.family) {
	case NV_DEVICE_INFO_V0_TNT:
82
		return NVIF_IOCTL_NEW_V0_SW_NV04;
83 84 85 86
	case NV_DEVICE_INFO_V0_CELSIUS:
	case NV_DEVICE_INFO_V0_KELVIN:
	case NV_DEVICE_INFO_V0_RANKINE:
	case NV_DEVICE_INFO_V0_CURIE:
87
		return NVIF_IOCTL_NEW_V0_SW_NV10;
88
	case NV_DEVICE_INFO_V0_TESLA:
89
		return NVIF_IOCTL_NEW_V0_SW_NV50;
90 91 92
	case NV_DEVICE_INFO_V0_FERMI:
	case NV_DEVICE_INFO_V0_KEPLER:
	case NV_DEVICE_INFO_V0_MAXWELL:
93
		return NVIF_IOCTL_NEW_V0_SW_GF100;
94 95 96 97 98 99 100 101 102
	}

	return 0x0000;
}

static void
nouveau_abi16_ntfy_fini(struct nouveau_abi16_chan *chan,
			struct nouveau_abi16_ntfy *ntfy)
{
103
	nvif_object_fini(&ntfy->object);
104
	nvkm_mm_free(&chan->heap, &ntfy->node);
105 106 107 108 109 110 111 112 113 114
	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;

115 116 117 118 119
	/* 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);

120 121 122 123 124 125 126
	/* 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);
127
		nouveau_bo_unpin(chan->ntfy);
128
		drm_gem_object_unreference_unlocked(&chan->ntfy->gem);
129 130 131
	}

	if (chan->heap.block_size)
132
		nvkm_mm_fini(&chan->heap);
133 134 135

	/* destroy channel object, all children will be killed too */
	if (chan->chan) {
136
		abi16->handles &= ~(1ULL << (chan->chan->user.handle & 0xffff));
137
		nouveau_channel_idle(chan->chan);
138 139 140 141 142 143 144 145 146 147
		nouveau_channel_del(&chan->chan);
	}

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

void
nouveau_abi16_fini(struct nouveau_abi16 *abi16)
{
148
	struct nouveau_cli *cli = (void *)abi16->device.object.client;
149 150 151 152 153 154 155 156
	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 */
157
	nvif_device_fini(&abi16->device);
158 159 160 161

	kfree(cli->abi16);
	cli->abi16 = NULL;
}
162 163 164 165

int
nouveau_abi16_ioctl_getparam(ABI16_IOCTL_ARGS)
{
166
	struct nouveau_cli *cli = nouveau_cli(file_priv);
167
	struct nouveau_drm *drm = nouveau_drm(dev);
168
	struct nvif_device *device = &drm->device;
169
	struct nvkm_gr *gr = nvxx_gr(device);
170 171 172 173
	struct drm_nouveau_getparam *getparam = data;

	switch (getparam->param) {
	case NOUVEAU_GETPARAM_CHIPSET_ID:
174
		getparam->value = device->info.chipset;
175 176
		break;
	case NOUVEAU_GETPARAM_PCI_VENDOR:
177
		if (nv_device_is_pci(nvxx_device(device)))
A
Alexandre Courbot 已提交
178 179 180
			getparam->value = dev->pdev->vendor;
		else
			getparam->value = 0;
181 182
		break;
	case NOUVEAU_GETPARAM_PCI_DEVICE:
183
		if (nv_device_is_pci(nvxx_device(device)))
A
Alexandre Courbot 已提交
184 185 186
			getparam->value = dev->pdev->device;
		else
			getparam->value = 0;
187 188
		break;
	case NOUVEAU_GETPARAM_BUS_TYPE:
189
		if (!nv_device_is_pci(nvxx_device(device)))
A
Alexandre Courbot 已提交
190 191
			getparam->value = 3;
		else
192 193 194 195 196 197 198 199 200
		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:
201
		getparam->value = drm->gem.vram_available;
202 203
		break;
	case NOUVEAU_GETPARAM_AGP_SIZE:
204
		getparam->value = drm->gem.gart_available;
205 206 207 208 209
		break;
	case NOUVEAU_GETPARAM_VM_VRAM_BASE:
		getparam->value = 0; /* deprecated */
		break;
	case NOUVEAU_GETPARAM_PTIMER_TIME:
210
		getparam->value = nvif_device_time(device);
211 212 213 214 215 216 217 218
		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:
219
		getparam->value = nvkm_gr_units(gr);
220
		break;
221
	default:
B
Ben Skeggs 已提交
222
		NV_PRINTK(dbg, cli, "unknown parameter %lld\n", getparam->param);
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
		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;
239 240 241 242
	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;
243
	struct nvif_device *device;
244 245
	int ret;

246 247
	if (unlikely(!abi16))
		return -ENOMEM;
248 249 250 251

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

252
	device = &abi16->device;
253

254
	/* hack to allow channel engine type specification on kepler */
255
	if (device->info.family >= NV_DEVICE_INFO_V0_KEPLER) {
256
		if (init->fb_ctxdma_handle != ~0)
257
			init->fb_ctxdma_handle = KEPLER_CHANNEL_GPFIFO_A_V0_ENGINE_GR;
258 259 260 261 262
		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;
263
		if (init->fb_ctxdma_handle == KEPLER_CHANNEL_GPFIFO_A_V0_ENGINE_GR)
264 265 266 267 268 269
			init->tt_ctxdma_handle = 1;
	}

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

270
	/* allocate "abi16 channel" data and make up a handle for it */
271 272
	init->channel = __ffs64(~abi16->handles);
	if (~abi16->handles == 0)
273 274 275 276 277 278 279 280
		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);
281
	abi16->handles |= (1ULL << init->channel);
282

283
	/* create channel object and initialise dma and fence management */
284 285
	ret = nouveau_channel_new(drm, device,
				  NOUVEAU_ABI16_CHAN(init->channel),
286
				  init->fb_ctxdma_handle,
287
				  init->tt_ctxdma_handle, &chan->chan);
288
	if (ret)
289 290
		goto done;

291
	if (device->info.family >= NV_DEVICE_INFO_V0_TESLA)
292 293 294 295
		init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_VRAM |
					NOUVEAU_GEM_DOMAIN_GART;
	else
	if (chan->chan->push.buffer->bo.mem.mem_type == TTM_PL_VRAM)
296
		init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_VRAM;
297 298
	else
		init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_GART;
299

300
	if (device->info.family < NV_DEVICE_INFO_V0_CELSIUS) {
301 302
		init->subchan[0].handle = 0x00000000;
		init->subchan[0].grclass = 0x0000;
303
		init->subchan[1].handle = chan->chan->nvsw.handle;
304
		init->subchan[1].grclass = 0x506e;
305 306 307 308
		init->nr_subchan = 2;
	}

	/* Named memory object area */
309 310 311
	ret = nouveau_gem_new(dev, PAGE_SIZE, 0, NOUVEAU_GEM_DOMAIN_GART,
			      0, 0, &chan->ntfy);
	if (ret == 0)
312
		ret = nouveau_bo_pin(chan->ntfy, TTM_PL_FLAG_TT, false);
313 314 315
	if (ret)
		goto done;

316
	if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
317
		ret = nouveau_bo_vma_add(chan->ntfy, cli->vm,
318 319 320 321 322
					&chan->ntfy_vma);
		if (ret)
			goto done;
	}

323
	ret = drm_gem_handle_create(file_priv, &chan->ntfy->gem,
324
				    &init->notifier_handle);
325 326
	if (ret)
		goto done;
327

328
	ret = nvkm_mm_init(&chan->heap, 0, PAGE_SIZE, 1);
329 330 331 332
done:
	if (ret)
		nouveau_abi16_chan_fini(abi16, chan);
	return nouveau_abi16_put(abi16, ret);
333 334
}

335 336 337 338 339 340
static struct nouveau_abi16_chan *
nouveau_abi16_chan(struct nouveau_abi16 *abi16, int channel)
{
	struct nouveau_abi16_chan *chan;

	list_for_each_entry(chan, &abi16->channels, head) {
341
		if (chan->chan->user.handle == NOUVEAU_ABI16_CHAN(channel))
342 343 344 345 346
			return chan;
	}

	return NULL;
}
347

348 349 350 351
int
nouveau_abi16_ioctl_channel_free(ABI16_IOCTL_ARGS)
{
	struct drm_nouveau_channel_free *req = data;
352 353
	struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
	struct nouveau_abi16_chan *chan;
354

355 356
	if (unlikely(!abi16))
		return -ENOMEM;
357

358 359 360 361 362
	chan = nouveau_abi16_chan(abi16, req->channel);
	if (!chan)
		return nouveau_abi16_put(abi16, -ENOENT);
	nouveau_abi16_chan_fini(abi16, chan);
	return nouveau_abi16_put(abi16, 0);
363 364 365 366 367 368
}

int
nouveau_abi16_ioctl_grobj_alloc(ABI16_IOCTL_ARGS)
{
	struct drm_nouveau_grobj_alloc *init = data;
369
	struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
370 371
	struct nouveau_abi16_chan *chan;
	struct nouveau_abi16_ntfy *ntfy;
372
	struct nvif_client *client;
373
	struct nvif_sclass *sclass;
374 375
	s32 oclass = 0;
	int ret, i;
376

377 378 379
	if (unlikely(!abi16))
		return -ENOMEM;

380
	if (init->handle == ~0)
381
		return nouveau_abi16_put(abi16, -EINVAL);
382
	client = abi16->device.object.client;
383

384 385 386 387
	chan = nouveau_abi16_chan(abi16, init->channel);
	if (!chan)
		return nouveau_abi16_put(abi16, -ENOENT);

388
	ret = nvif_object_sclass_get(&chan->chan->user, &sclass);
389 390 391 392 393 394
	if (ret < 0)
		return nouveau_abi16_put(abi16, ret);

	if ((init->class & 0x00ff) == 0x006e) {
		/* nvsw: compatibility with older 0x*6e class identifier */
		for (i = 0; !oclass && i < ret; i++) {
395
			switch (sclass[i].oclass) {
396 397 398 399
			case NVIF_IOCTL_NEW_V0_SW_NV04:
			case NVIF_IOCTL_NEW_V0_SW_NV10:
			case NVIF_IOCTL_NEW_V0_SW_NV50:
			case NVIF_IOCTL_NEW_V0_SW_GF100:
400
				oclass = sclass[i].oclass;
401 402 403 404 405 406 407 408 409
				break;
			default:
				break;
			}
		}
	} else
	if ((init->class & 0x00ff) == 0x00b1) {
		/* msvld: compatibility with incorrect version exposure */
		for (i = 0; i < ret; i++) {
410 411
			if ((sclass[i].oclass & 0x00ff) == 0x00b1) {
				oclass = sclass[i].oclass;
412 413 414 415 416 417 418
				break;
			}
		}
	} else
	if ((init->class & 0x00ff) == 0x00b2) { /* mspdec */
		/* mspdec: compatibility with incorrect version exposure */
		for (i = 0; i < ret; i++) {
419 420
			if ((sclass[i].oclass & 0x00ff) == 0x00b2) {
				oclass = sclass[i].oclass;
421 422 423 424 425 426 427
				break;
			}
		}
	} else
	if ((init->class & 0x00ff) == 0x00b3) { /* msppp */
		/* msppp: compatibility with incorrect version exposure */
		for (i = 0; i < ret; i++) {
428 429
			if ((sclass[i].oclass & 0x00ff) == 0x00b3) {
				oclass = sclass[i].oclass;
430 431 432 433 434 435 436
				break;
			}
		}
	} else {
		oclass = init->class;
	}

437
	nvif_object_sclass_put(&sclass);
438 439 440
	if (!oclass)
		return nouveau_abi16_put(abi16, -EINVAL);

441 442 443 444 445 446 447
	ntfy = kzalloc(sizeof(*ntfy), GFP_KERNEL);
	if (!ntfy)
		return nouveau_abi16_put(abi16, -ENOMEM);

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

	client->route = NVDRM_OBJECT_ABI16;
448
	ret = nvif_object_init(&chan->chan->user, init->handle, oclass,
449 450 451 452 453
			       NULL, 0, &ntfy->object);
	client->route = NVDRM_OBJECT_NVIF;

	if (ret)
		nouveau_abi16_ntfy_fini(chan, ntfy);
454
	return nouveau_abi16_put(abi16, ret);
455 456 457 458 459
}

int
nouveau_abi16_ioctl_notifierobj_alloc(ABI16_IOCTL_ARGS)
{
460 461 462
	struct drm_nouveau_notifierobj_alloc *info = data;
	struct nouveau_drm *drm = nouveau_drm(dev);
	struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
463
	struct nouveau_abi16_chan *chan;
464
	struct nouveau_abi16_ntfy *ntfy;
465
	struct nvif_device *device = &abi16->device;
466
	struct nvif_client *client;
467
	struct nv_dma_v0 args = {};
468 469
	int ret;

470 471 472
	if (unlikely(!abi16))
		return -ENOMEM;

473
	/* completely unnecessary for these chipsets... */
474
	if (unlikely(device->info.family >= NV_DEVICE_INFO_V0_FERMI))
475
		return nouveau_abi16_put(abi16, -EINVAL);
476
	client = abi16->device.object.client;
477

478
	chan = nouveau_abi16_chan(abi16, info->channel);
479 480 481 482 483 484 485 486 487
	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);

488 489
	ret = nvkm_mm_head(&chan->heap, 0, 1, info->size, info->size, 1,
			   &ntfy->node);
490 491 492
	if (ret)
		goto done;

493 494
	args.start = ntfy->node->offset;
	args.limit = ntfy->node->offset + ntfy->node->length - 1;
495
	if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
496 497 498 499
		args.target = NV_DMA_V0_TARGET_VM;
		args.access = NV_DMA_V0_ACCESS_VM;
		args.start += chan->ntfy_vma.offset;
		args.limit += chan->ntfy_vma.offset;
500 501
	} else
	if (drm->agp.stat == ENABLED) {
502 503 504 505
		args.target = NV_DMA_V0_TARGET_AGP;
		args.access = NV_DMA_V0_ACCESS_RDWR;
		args.start += drm->agp.base + chan->ntfy->bo.offset;
		args.limit += drm->agp.base + chan->ntfy->bo.offset;
506
	} else {
507 508 509 510
		args.target = NV_DMA_V0_TARGET_VM;
		args.access = NV_DMA_V0_ACCESS_RDWR;
		args.start += chan->ntfy->bo.offset;
		args.limit += chan->ntfy->bo.offset;
511 512
	}

513 514 515 516 517
	client->route = NVDRM_OBJECT_ABI16;
	client->super = true;
	ret = nvif_object_init(&chan->chan->user, info->handle,
			       NV_DMA_IN_MEMORY, &args, sizeof(args),
			       &ntfy->object);
518
	client->super = false;
519
	client->route = NVDRM_OBJECT_NVIF;
520 521 522
	if (ret)
		goto done;

523
	info->offset = ntfy->node->offset;
524 525 526 527
done:
	if (ret)
		nouveau_abi16_ntfy_fini(chan, ntfy);
	return nouveau_abi16_put(abi16, ret);
528 529 530 531 532
}

int
nouveau_abi16_ioctl_gpuobj_free(ABI16_IOCTL_ARGS)
{
533 534
	struct drm_nouveau_gpuobj_free *fini = data;
	struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
535
	struct nouveau_abi16_chan *chan;
536
	struct nouveau_abi16_ntfy *ntfy;
537
	int ret = -ENOENT;
538

539 540 541
	if (unlikely(!abi16))
		return -ENOMEM;

542
	chan = nouveau_abi16_chan(abi16, fini->channel);
543
	if (!chan)
544
		return nouveau_abi16_put(abi16, -EINVAL);
545

546 547
	/* synchronize with the user channel and destroy the gpu object */
	nouveau_channel_idle(chan->chan);
548

549
	list_for_each_entry(ntfy, &chan->notifiers, head) {
550 551 552
		if (ntfy->object.handle == fini->handle) {
			nouveau_abi16_ntfy_fini(chan, ntfy);
			ret = 0;
553 554 555 556
			break;
		}
	}

557
	return nouveau_abi16_put(abi16, ret);
558
}