xref: /linux/drivers/gpu/drm/nouveau/nouveau_abi16.c (revision e0a37f85fc95e3f2550446316bc4a27d00d75993)
1 /*
2  * Copyright 2012 Red Hat Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  */
23 
24 #include <nvif/client.h>
25 #include <nvif/driver.h>
26 #include <nvif/ioctl.h>
27 #include <nvif/class.h>
28 
29 #include "nouveau_drm.h"
30 #include "nouveau_dma.h"
31 #include "nouveau_gem.h"
32 #include "nouveau_chan.h"
33 #include "nouveau_abi16.h"
34 
35 struct nouveau_abi16 *
36 nouveau_abi16_get(struct drm_file *file_priv, struct drm_device *dev)
37 {
38 	struct nouveau_cli *cli = nouveau_cli(file_priv);
39 	mutex_lock(&cli->mutex);
40 	if (!cli->abi16) {
41 		struct nouveau_abi16 *abi16;
42 		cli->abi16 = abi16 = kzalloc(sizeof(*abi16), GFP_KERNEL);
43 		if (cli->abi16) {
44 			struct nv_device_v0 args = {
45 				.device = ~0ULL,
46 			};
47 
48 			INIT_LIST_HEAD(&abi16->channels);
49 
50 			/* allocate device object targeting client's default
51 			 * device (ie. the one that belongs to the fd it
52 			 * opened)
53 			 */
54 			if (nvif_device_init(&cli->base.object, 0, NV_DEVICE,
55 					     &args, sizeof(args),
56 					     &abi16->device) == 0)
57 				return cli->abi16;
58 
59 			kfree(cli->abi16);
60 			cli->abi16 = NULL;
61 		}
62 
63 		mutex_unlock(&cli->mutex);
64 	}
65 	return cli->abi16;
66 }
67 
68 int
69 nouveau_abi16_put(struct nouveau_abi16 *abi16, int ret)
70 {
71 	struct nouveau_cli *cli = (void *)abi16->device.object.client;
72 	mutex_unlock(&cli->mutex);
73 	return ret;
74 }
75 
76 s32
77 nouveau_abi16_swclass(struct nouveau_drm *drm)
78 {
79 	switch (drm->device.info.family) {
80 	case NV_DEVICE_INFO_V0_TNT:
81 		return NVIF_IOCTL_NEW_V0_SW_NV04;
82 	case NV_DEVICE_INFO_V0_CELSIUS:
83 	case NV_DEVICE_INFO_V0_KELVIN:
84 	case NV_DEVICE_INFO_V0_RANKINE:
85 	case NV_DEVICE_INFO_V0_CURIE:
86 		return NVIF_IOCTL_NEW_V0_SW_NV10;
87 	case NV_DEVICE_INFO_V0_TESLA:
88 		return NVIF_IOCTL_NEW_V0_SW_NV50;
89 	case NV_DEVICE_INFO_V0_FERMI:
90 	case NV_DEVICE_INFO_V0_KEPLER:
91 	case NV_DEVICE_INFO_V0_MAXWELL:
92 		return NVIF_IOCTL_NEW_V0_SW_GF100;
93 	}
94 
95 	return 0x0000;
96 }
97 
98 static void
99 nouveau_abi16_ntfy_fini(struct nouveau_abi16_chan *chan,
100 			struct nouveau_abi16_ntfy *ntfy)
101 {
102 	nvif_object_fini(&ntfy->object);
103 	nvkm_mm_free(&chan->heap, &ntfy->node);
104 	list_del(&ntfy->head);
105 	kfree(ntfy);
106 }
107 
108 static void
109 nouveau_abi16_chan_fini(struct nouveau_abi16 *abi16,
110 			struct nouveau_abi16_chan *chan)
111 {
112 	struct nouveau_abi16_ntfy *ntfy, *temp;
113 
114 	/* wait for all activity to stop before releasing notify object, which
115 	 * may be still in use */
116 	if (chan->chan && chan->ntfy)
117 		nouveau_channel_idle(chan->chan);
118 
119 	/* cleanup notifier state */
120 	list_for_each_entry_safe(ntfy, temp, &chan->notifiers, head) {
121 		nouveau_abi16_ntfy_fini(chan, ntfy);
122 	}
123 
124 	if (chan->ntfy) {
125 		nouveau_bo_vma_del(chan->ntfy, &chan->ntfy_vma);
126 		nouveau_bo_unpin(chan->ntfy);
127 		drm_gem_object_unreference_unlocked(&chan->ntfy->gem);
128 	}
129 
130 	if (chan->heap.block_size)
131 		nvkm_mm_fini(&chan->heap);
132 
133 	/* destroy channel object, all children will be killed too */
134 	if (chan->chan) {
135 		nouveau_channel_idle(chan->chan);
136 		nouveau_channel_del(&chan->chan);
137 	}
138 
139 	list_del(&chan->head);
140 	kfree(chan);
141 }
142 
143 void
144 nouveau_abi16_fini(struct nouveau_abi16 *abi16)
145 {
146 	struct nouveau_cli *cli = (void *)abi16->device.object.client;
147 	struct nouveau_abi16_chan *chan, *temp;
148 
149 	/* cleanup channels */
150 	list_for_each_entry_safe(chan, temp, &abi16->channels, head) {
151 		nouveau_abi16_chan_fini(abi16, chan);
152 	}
153 
154 	/* destroy the device object */
155 	nvif_device_fini(&abi16->device);
156 
157 	kfree(cli->abi16);
158 	cli->abi16 = NULL;
159 }
160 
161 int
162 nouveau_abi16_ioctl_getparam(ABI16_IOCTL_ARGS)
163 {
164 	struct nouveau_cli *cli = nouveau_cli(file_priv);
165 	struct nouveau_drm *drm = nouveau_drm(dev);
166 	struct nvif_device *device = &drm->device;
167 	struct nvkm_gr *gr = nvxx_gr(device);
168 	struct drm_nouveau_getparam *getparam = data;
169 
170 	switch (getparam->param) {
171 	case NOUVEAU_GETPARAM_CHIPSET_ID:
172 		getparam->value = device->info.chipset;
173 		break;
174 	case NOUVEAU_GETPARAM_PCI_VENDOR:
175 		if (nvxx_device(device)->func->pci)
176 			getparam->value = dev->pdev->vendor;
177 		else
178 			getparam->value = 0;
179 		break;
180 	case NOUVEAU_GETPARAM_PCI_DEVICE:
181 		if (nvxx_device(device)->func->pci)
182 			getparam->value = dev->pdev->device;
183 		else
184 			getparam->value = 0;
185 		break;
186 	case NOUVEAU_GETPARAM_BUS_TYPE:
187 		if (!nvxx_device(device)->func->pci)
188 			getparam->value = 3;
189 		else
190 		if (drm_pci_device_is_agp(dev))
191 			getparam->value = 0;
192 		else
193 		if (!pci_is_pcie(dev->pdev))
194 			getparam->value = 1;
195 		else
196 			getparam->value = 2;
197 		break;
198 	case NOUVEAU_GETPARAM_FB_SIZE:
199 		getparam->value = drm->gem.vram_available;
200 		break;
201 	case NOUVEAU_GETPARAM_AGP_SIZE:
202 		getparam->value = drm->gem.gart_available;
203 		break;
204 	case NOUVEAU_GETPARAM_VM_VRAM_BASE:
205 		getparam->value = 0; /* deprecated */
206 		break;
207 	case NOUVEAU_GETPARAM_PTIMER_TIME:
208 		getparam->value = nvif_device_time(device);
209 		break;
210 	case NOUVEAU_GETPARAM_HAS_BO_USAGE:
211 		getparam->value = 1;
212 		break;
213 	case NOUVEAU_GETPARAM_HAS_PAGEFLIP:
214 		getparam->value = 1;
215 		break;
216 	case NOUVEAU_GETPARAM_GRAPH_UNITS:
217 		getparam->value = nvkm_gr_units(gr);
218 		break;
219 	default:
220 		NV_PRINTK(dbg, cli, "unknown parameter %lld\n", getparam->param);
221 		return -EINVAL;
222 	}
223 
224 	return 0;
225 }
226 
227 int
228 nouveau_abi16_ioctl_setparam(ABI16_IOCTL_ARGS)
229 {
230 	return -EINVAL;
231 }
232 
233 int
234 nouveau_abi16_ioctl_channel_alloc(ABI16_IOCTL_ARGS)
235 {
236 	struct drm_nouveau_channel_alloc *init = data;
237 	struct nouveau_cli *cli = nouveau_cli(file_priv);
238 	struct nouveau_drm *drm = nouveau_drm(dev);
239 	struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
240 	struct nouveau_abi16_chan *chan;
241 	struct nvif_device *device;
242 	int ret;
243 
244 	if (unlikely(!abi16))
245 		return -ENOMEM;
246 
247 	if (!drm->channel)
248 		return nouveau_abi16_put(abi16, -ENODEV);
249 
250 	device = &abi16->device;
251 
252 	/* hack to allow channel engine type specification on kepler */
253 	if (device->info.family >= NV_DEVICE_INFO_V0_KEPLER) {
254 		if (init->fb_ctxdma_handle != ~0)
255 			init->fb_ctxdma_handle = KEPLER_CHANNEL_GPFIFO_A_V0_ENGINE_GR;
256 		else
257 			init->fb_ctxdma_handle = init->tt_ctxdma_handle;
258 
259 		/* allow flips to be executed if this is a graphics channel */
260 		init->tt_ctxdma_handle = 0;
261 		if (init->fb_ctxdma_handle == KEPLER_CHANNEL_GPFIFO_A_V0_ENGINE_GR)
262 			init->tt_ctxdma_handle = 1;
263 	}
264 
265 	if (init->fb_ctxdma_handle == ~0 || init->tt_ctxdma_handle == ~0)
266 		return nouveau_abi16_put(abi16, -EINVAL);
267 
268 	/* allocate "abi16 channel" data and make up a handle for it */
269 	chan = kzalloc(sizeof(*chan), GFP_KERNEL);
270 	if (!chan)
271 		return nouveau_abi16_put(abi16, -ENOMEM);
272 
273 	INIT_LIST_HEAD(&chan->notifiers);
274 	list_add(&chan->head, &abi16->channels);
275 
276 	/* create channel object and initialise dma and fence management */
277 	ret = nouveau_channel_new(drm, device, init->fb_ctxdma_handle,
278 				  init->tt_ctxdma_handle, &chan->chan);
279 	if (ret)
280 		goto done;
281 
282 	init->channel = chan->chan->chid;
283 
284 	if (device->info.family >= NV_DEVICE_INFO_V0_TESLA)
285 		init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_VRAM |
286 					NOUVEAU_GEM_DOMAIN_GART;
287 	else
288 	if (chan->chan->push.buffer->bo.mem.mem_type == TTM_PL_VRAM)
289 		init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_VRAM;
290 	else
291 		init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_GART;
292 
293 	if (device->info.family < NV_DEVICE_INFO_V0_CELSIUS) {
294 		init->subchan[0].handle = 0x00000000;
295 		init->subchan[0].grclass = 0x0000;
296 		init->subchan[1].handle = chan->chan->nvsw.handle;
297 		init->subchan[1].grclass = 0x506e;
298 		init->nr_subchan = 2;
299 	}
300 
301 	/* Named memory object area */
302 	ret = nouveau_gem_new(dev, PAGE_SIZE, 0, NOUVEAU_GEM_DOMAIN_GART,
303 			      0, 0, &chan->ntfy);
304 	if (ret == 0)
305 		ret = nouveau_bo_pin(chan->ntfy, TTM_PL_FLAG_TT, false);
306 	if (ret)
307 		goto done;
308 
309 	if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
310 		ret = nouveau_bo_vma_add(chan->ntfy, cli->vm,
311 					&chan->ntfy_vma);
312 		if (ret)
313 			goto done;
314 	}
315 
316 	ret = drm_gem_handle_create(file_priv, &chan->ntfy->gem,
317 				    &init->notifier_handle);
318 	if (ret)
319 		goto done;
320 
321 	ret = nvkm_mm_init(&chan->heap, 0, PAGE_SIZE, 1);
322 done:
323 	if (ret)
324 		nouveau_abi16_chan_fini(abi16, chan);
325 	return nouveau_abi16_put(abi16, ret);
326 }
327 
328 static struct nouveau_abi16_chan *
329 nouveau_abi16_chan(struct nouveau_abi16 *abi16, int channel)
330 {
331 	struct nouveau_abi16_chan *chan;
332 
333 	list_for_each_entry(chan, &abi16->channels, head) {
334 		if (chan->chan->chid == channel)
335 			return chan;
336 	}
337 
338 	return NULL;
339 }
340 
341 int
342 nouveau_abi16_ioctl_channel_free(ABI16_IOCTL_ARGS)
343 {
344 	struct drm_nouveau_channel_free *req = data;
345 	struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
346 	struct nouveau_abi16_chan *chan;
347 
348 	if (unlikely(!abi16))
349 		return -ENOMEM;
350 
351 	chan = nouveau_abi16_chan(abi16, req->channel);
352 	if (!chan)
353 		return nouveau_abi16_put(abi16, -ENOENT);
354 	nouveau_abi16_chan_fini(abi16, chan);
355 	return nouveau_abi16_put(abi16, 0);
356 }
357 
358 int
359 nouveau_abi16_ioctl_grobj_alloc(ABI16_IOCTL_ARGS)
360 {
361 	struct drm_nouveau_grobj_alloc *init = data;
362 	struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
363 	struct nouveau_abi16_chan *chan;
364 	struct nouveau_abi16_ntfy *ntfy;
365 	struct nvif_client *client;
366 	struct nvif_sclass *sclass;
367 	s32 oclass = 0;
368 	int ret, i;
369 
370 	if (unlikely(!abi16))
371 		return -ENOMEM;
372 
373 	if (init->handle == ~0)
374 		return nouveau_abi16_put(abi16, -EINVAL);
375 	client = abi16->device.object.client;
376 
377 	chan = nouveau_abi16_chan(abi16, init->channel);
378 	if (!chan)
379 		return nouveau_abi16_put(abi16, -ENOENT);
380 
381 	ret = nvif_object_sclass_get(&chan->chan->user, &sclass);
382 	if (ret < 0)
383 		return nouveau_abi16_put(abi16, ret);
384 
385 	if ((init->class & 0x00ff) == 0x006e) {
386 		/* nvsw: compatibility with older 0x*6e class identifier */
387 		for (i = 0; !oclass && i < ret; i++) {
388 			switch (sclass[i].oclass) {
389 			case NVIF_IOCTL_NEW_V0_SW_NV04:
390 			case NVIF_IOCTL_NEW_V0_SW_NV10:
391 			case NVIF_IOCTL_NEW_V0_SW_NV50:
392 			case NVIF_IOCTL_NEW_V0_SW_GF100:
393 				oclass = sclass[i].oclass;
394 				break;
395 			default:
396 				break;
397 			}
398 		}
399 	} else
400 	if ((init->class & 0x00ff) == 0x00b1) {
401 		/* msvld: compatibility with incorrect version exposure */
402 		for (i = 0; i < ret; i++) {
403 			if ((sclass[i].oclass & 0x00ff) == 0x00b1) {
404 				oclass = sclass[i].oclass;
405 				break;
406 			}
407 		}
408 	} else
409 	if ((init->class & 0x00ff) == 0x00b2) { /* mspdec */
410 		/* mspdec: compatibility with incorrect version exposure */
411 		for (i = 0; i < ret; i++) {
412 			if ((sclass[i].oclass & 0x00ff) == 0x00b2) {
413 				oclass = sclass[i].oclass;
414 				break;
415 			}
416 		}
417 	} else
418 	if ((init->class & 0x00ff) == 0x00b3) { /* msppp */
419 		/* msppp: compatibility with incorrect version exposure */
420 		for (i = 0; i < ret; i++) {
421 			if ((sclass[i].oclass & 0x00ff) == 0x00b3) {
422 				oclass = sclass[i].oclass;
423 				break;
424 			}
425 		}
426 	} else {
427 		oclass = init->class;
428 	}
429 
430 	nvif_object_sclass_put(&sclass);
431 	if (!oclass)
432 		return nouveau_abi16_put(abi16, -EINVAL);
433 
434 	ntfy = kzalloc(sizeof(*ntfy), GFP_KERNEL);
435 	if (!ntfy)
436 		return nouveau_abi16_put(abi16, -ENOMEM);
437 
438 	list_add(&ntfy->head, &chan->notifiers);
439 
440 	client->route = NVDRM_OBJECT_ABI16;
441 	ret = nvif_object_init(&chan->chan->user, init->handle, oclass,
442 			       NULL, 0, &ntfy->object);
443 	client->route = NVDRM_OBJECT_NVIF;
444 
445 	if (ret)
446 		nouveau_abi16_ntfy_fini(chan, ntfy);
447 	return nouveau_abi16_put(abi16, ret);
448 }
449 
450 int
451 nouveau_abi16_ioctl_notifierobj_alloc(ABI16_IOCTL_ARGS)
452 {
453 	struct drm_nouveau_notifierobj_alloc *info = data;
454 	struct nouveau_drm *drm = nouveau_drm(dev);
455 	struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
456 	struct nouveau_abi16_chan *chan;
457 	struct nouveau_abi16_ntfy *ntfy;
458 	struct nvif_device *device = &abi16->device;
459 	struct nvif_client *client;
460 	struct nv_dma_v0 args = {};
461 	int ret;
462 
463 	if (unlikely(!abi16))
464 		return -ENOMEM;
465 
466 	/* completely unnecessary for these chipsets... */
467 	if (unlikely(device->info.family >= NV_DEVICE_INFO_V0_FERMI))
468 		return nouveau_abi16_put(abi16, -EINVAL);
469 	client = abi16->device.object.client;
470 
471 	chan = nouveau_abi16_chan(abi16, info->channel);
472 	if (!chan)
473 		return nouveau_abi16_put(abi16, -ENOENT);
474 
475 	ntfy = kzalloc(sizeof(*ntfy), GFP_KERNEL);
476 	if (!ntfy)
477 		return nouveau_abi16_put(abi16, -ENOMEM);
478 
479 	list_add(&ntfy->head, &chan->notifiers);
480 
481 	ret = nvkm_mm_head(&chan->heap, 0, 1, info->size, info->size, 1,
482 			   &ntfy->node);
483 	if (ret)
484 		goto done;
485 
486 	args.start = ntfy->node->offset;
487 	args.limit = ntfy->node->offset + ntfy->node->length - 1;
488 	if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
489 		args.target = NV_DMA_V0_TARGET_VM;
490 		args.access = NV_DMA_V0_ACCESS_VM;
491 		args.start += chan->ntfy_vma.offset;
492 		args.limit += chan->ntfy_vma.offset;
493 	} else
494 	if (drm->agp.bridge) {
495 		args.target = NV_DMA_V0_TARGET_AGP;
496 		args.access = NV_DMA_V0_ACCESS_RDWR;
497 		args.start += drm->agp.base + chan->ntfy->bo.offset;
498 		args.limit += drm->agp.base + chan->ntfy->bo.offset;
499 	} else {
500 		args.target = NV_DMA_V0_TARGET_VM;
501 		args.access = NV_DMA_V0_ACCESS_RDWR;
502 		args.start += chan->ntfy->bo.offset;
503 		args.limit += chan->ntfy->bo.offset;
504 	}
505 
506 	client->route = NVDRM_OBJECT_ABI16;
507 	client->super = true;
508 	ret = nvif_object_init(&chan->chan->user, info->handle,
509 			       NV_DMA_IN_MEMORY, &args, sizeof(args),
510 			       &ntfy->object);
511 	client->super = false;
512 	client->route = NVDRM_OBJECT_NVIF;
513 	if (ret)
514 		goto done;
515 
516 	info->offset = ntfy->node->offset;
517 done:
518 	if (ret)
519 		nouveau_abi16_ntfy_fini(chan, ntfy);
520 	return nouveau_abi16_put(abi16, ret);
521 }
522 
523 int
524 nouveau_abi16_ioctl_gpuobj_free(ABI16_IOCTL_ARGS)
525 {
526 	struct drm_nouveau_gpuobj_free *fini = data;
527 	struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
528 	struct nouveau_abi16_chan *chan;
529 	struct nouveau_abi16_ntfy *ntfy;
530 	int ret = -ENOENT;
531 
532 	if (unlikely(!abi16))
533 		return -ENOMEM;
534 
535 	chan = nouveau_abi16_chan(abi16, fini->channel);
536 	if (!chan)
537 		return nouveau_abi16_put(abi16, -EINVAL);
538 
539 	/* synchronize with the user channel and destroy the gpu object */
540 	nouveau_channel_idle(chan->chan);
541 
542 	list_for_each_entry(ntfy, &chan->notifiers, head) {
543 		if (ntfy->object.handle == fini->handle) {
544 			nouveau_abi16_ntfy_fini(chan, ntfy);
545 			ret = 0;
546 			break;
547 		}
548 	}
549 
550 	return nouveau_abi16_put(abi16, ret);
551 }
552