nouveau_abi16.c 14.1 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 44 45 46 47 48 49

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) {
			INIT_LIST_HEAD(&abi16->channels);

			/* allocate device object targeting client's default
			 * device (ie. the one that belongs to the fd it
			 * opened)
			 */
50
			if (nvif_device_init(&cli->base.base, NULL,
51
					     NOUVEAU_ABI16_DEVICE, NV_DEVICE,
52
					     &(struct nv_device_class) {
53
						.device = ~0ULL,
54 55
					     }, sizeof(struct nv_device_class),
					     &abi16->device) == 0)
56 57 58 59 60 61 62 63 64 65 66 67 68 69
				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)
{
70
	struct nouveau_cli *cli = (void *)nvif_client(&abi16->device.base);
71 72 73 74 75 76 77
	mutex_unlock(&cli->mutex);
	return ret;
}

u16
nouveau_abi16_swclass(struct nouveau_drm *drm)
{
78 79
	switch (drm->device.info.family) {
	case NV_DEVICE_INFO_V0_TNT:
80
		return 0x006e;
81 82 83 84
	case NV_DEVICE_INFO_V0_CELSIUS:
	case NV_DEVICE_INFO_V0_KELVIN:
	case NV_DEVICE_INFO_V0_RANKINE:
	case NV_DEVICE_INFO_V0_CURIE:
85
		return 0x016e;
86
	case NV_DEVICE_INFO_V0_TESLA:
87
		return 0x506e;
88 89 90
	case NV_DEVICE_INFO_V0_FERMI:
	case NV_DEVICE_INFO_V0_KEPLER:
	case NV_DEVICE_INFO_V0_MAXWELL:
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
		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;

112 113 114 115 116
	/* 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);

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

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

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

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

void
nouveau_abi16_fini(struct nouveau_abi16 *abi16)
{
144
	struct nouveau_cli *cli = (void *)nvif_client(&abi16->device.base);
145 146 147 148 149 150 151 152
	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 */
153
	nvif_device_fini(&abi16->device);
154 155 156 157

	kfree(cli->abi16);
	cli->abi16 = NULL;
}
158 159 160 161

int
nouveau_abi16_ioctl_getparam(ABI16_IOCTL_ARGS)
{
162
	struct nouveau_cli *cli = nouveau_cli(file_priv);
163
	struct nouveau_drm *drm = nouveau_drm(dev);
164 165 166
	struct nvif_device *device = &drm->device;
	struct nouveau_timer *ptimer = nvkm_timer(device);
	struct nouveau_graph *graph = nvkm_gr(device);
167 168 169 170
	struct drm_nouveau_getparam *getparam = data;

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

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

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

251 252 253
	device = &abi16->device;
	imem   = nvkm_instmem(device);
	pfb    = nvkm_fb(device);
254

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

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

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

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

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

	/* Named memory object area */
310 311 312 313 314 315 316
	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;

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

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

329 330 331 332 333
	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);
334 335
}

336 337 338 339 340 341 342 343 344 345 346 347
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) {
		if (chan->chan->object->handle == NOUVEAU_ABI16_CHAN(channel))
			return chan;
	}

	return NULL;
}
348

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

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

359 360 361 362 363
	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);
364 365 366 367 368 369
}

int
nouveau_abi16_ioctl_grobj_alloc(ABI16_IOCTL_ARGS)
{
	struct drm_nouveau_grobj_alloc *init = data;
370 371 372 373 374 375 376 377 378 379 380 381 382 383
	struct {
		struct nvif_ioctl_v0 ioctl;
		struct nvif_ioctl_new_v0 new;
	} args = {
		.ioctl.owner = NVIF_IOCTL_V0_OWNER_ANY,
		.ioctl.type = NVIF_IOCTL_V0_NEW,
		.ioctl.path_nr = 3,
		.ioctl.path[2] = NOUVEAU_ABI16_CLIENT,
		.ioctl.path[1] = NOUVEAU_ABI16_DEVICE,
		.ioctl.path[0] = NOUVEAU_ABI16_CHAN(init->channel),
		.new.route = NVDRM_OBJECT_ABI16,
		.new.handle = init->handle,
		.new.oclass = init->class,
	};
384 385
	struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
	struct nouveau_drm *drm = nouveau_drm(dev);
386
	struct nvif_client *client;
387 388
	int ret;

389 390 391
	if (unlikely(!abi16))
		return -ENOMEM;

392
	if (init->handle == ~0)
393
		return nouveau_abi16_put(abi16, -EINVAL);
394
	client = nvif_client(nvif_object(&abi16->device));
395 396 397

	/* compatibility with userspace that assumes 506e for all chipsets */
	if (init->class == 0x506e) {
398
		init->class = nouveau_abi16_swclass(drm);
399
		if (init->class == 0x906e)
400
			return nouveau_abi16_put(abi16, 0);
401 402
	}

403
	ret = nvif_client_ioctl(client, &args, sizeof(args));
404
	return nouveau_abi16_put(abi16, ret);
405 406 407 408 409
}

int
nouveau_abi16_ioctl_notifierobj_alloc(ABI16_IOCTL_ARGS)
{
410
	struct drm_nouveau_notifierobj_alloc *info = data;
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
	struct {
		struct nvif_ioctl_v0 ioctl;
		struct nvif_ioctl_new_v0 new;
		struct nv_dma_class ctxdma;
	} args = {
		.ioctl.owner = NVIF_IOCTL_V0_OWNER_ANY,
		.ioctl.type = NVIF_IOCTL_V0_NEW,
		.ioctl.path_nr = 3,
		.ioctl.path[2] = NOUVEAU_ABI16_CLIENT,
		.ioctl.path[1] = NOUVEAU_ABI16_DEVICE,
		.ioctl.path[0] = NOUVEAU_ABI16_CHAN(info->channel),
		.new.route = NVDRM_OBJECT_ABI16,
		.new.handle = info->handle,
		.new.oclass = NV_DMA_IN_MEMORY_CLASS,
	};
426 427
	struct nouveau_drm *drm = nouveau_drm(dev);
	struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
428
	struct nouveau_abi16_chan *chan;
429
	struct nouveau_abi16_ntfy *ntfy;
430
	struct nvif_device *device = &abi16->device;
431
	struct nvif_client *client;
432 433
	int ret;

434 435 436
	if (unlikely(!abi16))
		return -ENOMEM;

437
	/* completely unnecessary for these chipsets... */
438
	if (unlikely(device->info.family >= NV_DEVICE_INFO_V0_FERMI))
439
		return nouveau_abi16_put(abi16, -EINVAL);
440
	client = nvif_client(nvif_object(&abi16->device));
441

442
	chan = nouveau_abi16_chan(abi16, info->channel);
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
	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;

458 459
	args.ctxdma.start = ntfy->node->offset;
	args.ctxdma.limit = ntfy->node->offset + ntfy->node->length - 1;
460
	if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
461 462 463
		args.ctxdma.flags  = NV_DMA_TARGET_VM | NV_DMA_ACCESS_VM;
		args.ctxdma.start += chan->ntfy_vma.offset;
		args.ctxdma.limit += chan->ntfy_vma.offset;
464 465
	} else
	if (drm->agp.stat == ENABLED) {
466 467 468 469
		args.ctxdma.flags  = NV_DMA_TARGET_AGP | NV_DMA_ACCESS_RDWR;
		args.ctxdma.start += drm->agp.base + chan->ntfy->bo.offset;
		args.ctxdma.limit += drm->agp.base + chan->ntfy->bo.offset;
		client->super = true;
470
	} else {
471 472 473
		args.ctxdma.flags  = NV_DMA_TARGET_VM | NV_DMA_ACCESS_RDWR;
		args.ctxdma.start += chan->ntfy->bo.offset;
		args.ctxdma.limit += chan->ntfy->bo.offset;
474 475
	}

476 477
	ret = nvif_client_ioctl(client, &args, sizeof(args));
	client->super = false;
478 479 480
	if (ret)
		goto done;

481 482
	info->offset = ntfy->node->offset;

483 484 485 486
done:
	if (ret)
		nouveau_abi16_ntfy_fini(chan, ntfy);
	return nouveau_abi16_put(abi16, ret);
487 488 489 490 491
}

int
nouveau_abi16_ioctl_gpuobj_free(ABI16_IOCTL_ARGS)
{
492 493
	struct drm_nouveau_gpuobj_free *fini = data;
	struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
494
	struct nouveau_abi16_chan *chan;
495
	struct nouveau_abi16_ntfy *ntfy;
496 497
	int ret;

498 499 500
	if (unlikely(!abi16))
		return -ENOMEM;

501
	chan = nouveau_abi16_chan(abi16, fini->channel);
502 503
	if (!chan)
		return nouveau_abi16_put(abi16, -ENOENT);
504

505 506
	/* synchronize with the user channel and destroy the gpu object */
	nouveau_channel_idle(chan->chan);
507

508 509 510
	/*XXX*/
	ret = nouveau_object_del(nv_object(nvkm_client(&abi16->device.base)),
				 chan->chan->object->handle, fini->handle);
511 512 513 514 515 516 517 518 519 520 521 522 523
	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);
524
}