]> git.ipfire.org Git - thirdparty/linux.git/blob - drivers/gpu/drm/nouveau/nouveau_abi16.c
ea2472770b21bed407cfc1de27fbd1bc639b80de
[thirdparty/linux.git] / drivers / gpu / drm / nouveau / nouveau_abi16.c
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/fifo.h>
27 #include <nvif/ioctl.h>
28 #include <nvif/class.h>
29 #include <nvif/cl0002.h>
30 #include <nvif/cla06f.h>
31 #include <nvif/unpack.h>
32
33 #include "nouveau_drv.h"
34 #include "nouveau_dma.h"
35 #include "nouveau_gem.h"
36 #include "nouveau_chan.h"
37 #include "nouveau_abi16.h"
38 #include "nouveau_vmm.h"
39
40 static struct nouveau_abi16 *
41 nouveau_abi16(struct drm_file *file_priv)
42 {
43 struct nouveau_cli *cli = nouveau_cli(file_priv);
44 if (!cli->abi16) {
45 struct nouveau_abi16 *abi16;
46 cli->abi16 = abi16 = kzalloc(sizeof(*abi16), GFP_KERNEL);
47 if (cli->abi16) {
48 struct nv_device_v0 args = {
49 .device = ~0ULL,
50 };
51
52 INIT_LIST_HEAD(&abi16->channels);
53
54 /* allocate device object targeting client's default
55 * device (ie. the one that belongs to the fd it
56 * opened)
57 */
58 if (nvif_device_init(&cli->base.object, 0, NV_DEVICE,
59 &args, sizeof(args),
60 &abi16->device) == 0)
61 return cli->abi16;
62
63 kfree(cli->abi16);
64 cli->abi16 = NULL;
65 }
66 }
67 return cli->abi16;
68 }
69
70 struct nouveau_abi16 *
71 nouveau_abi16_get(struct drm_file *file_priv)
72 {
73 struct nouveau_cli *cli = nouveau_cli(file_priv);
74 mutex_lock(&cli->mutex);
75 if (nouveau_abi16(file_priv))
76 return cli->abi16;
77 mutex_unlock(&cli->mutex);
78 return NULL;
79 }
80
81 int
82 nouveau_abi16_put(struct nouveau_abi16 *abi16, int ret)
83 {
84 struct nouveau_cli *cli = (void *)abi16->device.object.client;
85 mutex_unlock(&cli->mutex);
86 return ret;
87 }
88
89 s32
90 nouveau_abi16_swclass(struct nouveau_drm *drm)
91 {
92 switch (drm->client.device.info.family) {
93 case NV_DEVICE_INFO_V0_TNT:
94 return NVIF_CLASS_SW_NV04;
95 case NV_DEVICE_INFO_V0_CELSIUS:
96 case NV_DEVICE_INFO_V0_KELVIN:
97 case NV_DEVICE_INFO_V0_RANKINE:
98 case NV_DEVICE_INFO_V0_CURIE:
99 return NVIF_CLASS_SW_NV10;
100 case NV_DEVICE_INFO_V0_TESLA:
101 return NVIF_CLASS_SW_NV50;
102 case NV_DEVICE_INFO_V0_FERMI:
103 case NV_DEVICE_INFO_V0_KEPLER:
104 case NV_DEVICE_INFO_V0_MAXWELL:
105 case NV_DEVICE_INFO_V0_PASCAL:
106 return NVIF_CLASS_SW_GF100;
107 }
108
109 return 0x0000;
110 }
111
112 static void
113 nouveau_abi16_ntfy_fini(struct nouveau_abi16_chan *chan,
114 struct nouveau_abi16_ntfy *ntfy)
115 {
116 nvif_object_fini(&ntfy->object);
117 nvkm_mm_free(&chan->heap, &ntfy->node);
118 list_del(&ntfy->head);
119 kfree(ntfy);
120 }
121
122 static void
123 nouveau_abi16_chan_fini(struct nouveau_abi16 *abi16,
124 struct nouveau_abi16_chan *chan)
125 {
126 struct nouveau_abi16_ntfy *ntfy, *temp;
127
128 /* wait for all activity to stop before releasing notify object, which
129 * may be still in use */
130 if (chan->chan && chan->ntfy)
131 nouveau_channel_idle(chan->chan);
132
133 /* cleanup notifier state */
134 list_for_each_entry_safe(ntfy, temp, &chan->notifiers, head) {
135 nouveau_abi16_ntfy_fini(chan, ntfy);
136 }
137
138 if (chan->ntfy) {
139 nouveau_vma_del(&chan->ntfy_vma);
140 nouveau_bo_unpin(chan->ntfy);
141 drm_gem_object_unreference_unlocked(&chan->ntfy->gem);
142 }
143
144 if (chan->heap.block_size)
145 nvkm_mm_fini(&chan->heap);
146
147 /* destroy channel object, all children will be killed too */
148 if (chan->chan) {
149 nouveau_channel_idle(chan->chan);
150 nouveau_channel_del(&chan->chan);
151 }
152
153 list_del(&chan->head);
154 kfree(chan);
155 }
156
157 void
158 nouveau_abi16_fini(struct nouveau_abi16 *abi16)
159 {
160 struct nouveau_cli *cli = (void *)abi16->device.object.client;
161 struct nouveau_abi16_chan *chan, *temp;
162
163 /* cleanup channels */
164 list_for_each_entry_safe(chan, temp, &abi16->channels, head) {
165 nouveau_abi16_chan_fini(abi16, chan);
166 }
167
168 /* destroy the device object */
169 nvif_device_fini(&abi16->device);
170
171 kfree(cli->abi16);
172 cli->abi16 = NULL;
173 }
174
175 int
176 nouveau_abi16_ioctl_getparam(ABI16_IOCTL_ARGS)
177 {
178 struct nouveau_cli *cli = nouveau_cli(file_priv);
179 struct nouveau_drm *drm = nouveau_drm(dev);
180 struct nvif_device *device = &drm->client.device;
181 struct nvkm_gr *gr = nvxx_gr(device);
182 struct drm_nouveau_getparam *getparam = data;
183
184 switch (getparam->param) {
185 case NOUVEAU_GETPARAM_CHIPSET_ID:
186 getparam->value = device->info.chipset;
187 break;
188 case NOUVEAU_GETPARAM_PCI_VENDOR:
189 if (device->info.platform != NV_DEVICE_INFO_V0_SOC)
190 getparam->value = dev->pdev->vendor;
191 else
192 getparam->value = 0;
193 break;
194 case NOUVEAU_GETPARAM_PCI_DEVICE:
195 if (device->info.platform != NV_DEVICE_INFO_V0_SOC)
196 getparam->value = dev->pdev->device;
197 else
198 getparam->value = 0;
199 break;
200 case NOUVEAU_GETPARAM_BUS_TYPE:
201 switch (device->info.platform) {
202 case NV_DEVICE_INFO_V0_AGP : getparam->value = 0; break;
203 case NV_DEVICE_INFO_V0_PCI : getparam->value = 1; break;
204 case NV_DEVICE_INFO_V0_PCIE: getparam->value = 2; break;
205 case NV_DEVICE_INFO_V0_SOC : getparam->value = 3; break;
206 case NV_DEVICE_INFO_V0_IGP :
207 if (!pci_is_pcie(dev->pdev))
208 getparam->value = 1;
209 else
210 getparam->value = 2;
211 break;
212 default:
213 WARN_ON(1);
214 break;
215 }
216 case NOUVEAU_GETPARAM_FB_SIZE:
217 getparam->value = drm->gem.vram_available;
218 break;
219 case NOUVEAU_GETPARAM_AGP_SIZE:
220 getparam->value = drm->gem.gart_available;
221 break;
222 case NOUVEAU_GETPARAM_VM_VRAM_BASE:
223 getparam->value = 0; /* deprecated */
224 break;
225 case NOUVEAU_GETPARAM_PTIMER_TIME:
226 getparam->value = nvif_device_time(device);
227 break;
228 case NOUVEAU_GETPARAM_HAS_BO_USAGE:
229 getparam->value = 1;
230 break;
231 case NOUVEAU_GETPARAM_HAS_PAGEFLIP:
232 getparam->value = 1;
233 break;
234 case NOUVEAU_GETPARAM_GRAPH_UNITS:
235 getparam->value = nvkm_gr_units(gr);
236 break;
237 default:
238 NV_PRINTK(dbg, cli, "unknown parameter %lld\n", getparam->param);
239 return -EINVAL;
240 }
241
242 return 0;
243 }
244
245 int
246 nouveau_abi16_ioctl_setparam(ABI16_IOCTL_ARGS)
247 {
248 return -EINVAL;
249 }
250
251 int
252 nouveau_abi16_ioctl_channel_alloc(ABI16_IOCTL_ARGS)
253 {
254 struct drm_nouveau_channel_alloc *init = data;
255 struct nouveau_cli *cli = nouveau_cli(file_priv);
256 struct nouveau_drm *drm = nouveau_drm(dev);
257 struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv);
258 struct nouveau_abi16_chan *chan;
259 struct nvif_device *device;
260 u64 engine;
261 int ret;
262
263 if (unlikely(!abi16))
264 return -ENOMEM;
265
266 if (!drm->channel)
267 return nouveau_abi16_put(abi16, -ENODEV);
268
269 device = &abi16->device;
270
271 /* hack to allow channel engine type specification on kepler */
272 if (device->info.family >= NV_DEVICE_INFO_V0_KEPLER) {
273 if (init->fb_ctxdma_handle == ~0) {
274 switch (init->tt_ctxdma_handle) {
275 case 0x01: engine = NV_DEVICE_INFO_ENGINE_GR ; break;
276 case 0x02: engine = NV_DEVICE_INFO_ENGINE_MSPDEC; break;
277 case 0x04: engine = NV_DEVICE_INFO_ENGINE_MSPPP ; break;
278 case 0x08: engine = NV_DEVICE_INFO_ENGINE_MSVLD ; break;
279 case 0x30: engine = NV_DEVICE_INFO_ENGINE_CE ; break;
280 default:
281 return nouveau_abi16_put(abi16, -ENOSYS);
282 }
283 } else {
284 engine = NV_DEVICE_INFO_ENGINE_GR;
285 }
286
287 if (engine != NV_DEVICE_INFO_ENGINE_CE)
288 engine = nvif_fifo_runlist(device, engine);
289 else
290 engine = nvif_fifo_runlist_ce(device);
291 init->fb_ctxdma_handle = engine;
292 init->tt_ctxdma_handle = 0;
293 }
294
295 if (init->fb_ctxdma_handle == ~0 || init->tt_ctxdma_handle == ~0)
296 return nouveau_abi16_put(abi16, -EINVAL);
297
298 /* allocate "abi16 channel" data and make up a handle for it */
299 chan = kzalloc(sizeof(*chan), GFP_KERNEL);
300 if (!chan)
301 return nouveau_abi16_put(abi16, -ENOMEM);
302
303 INIT_LIST_HEAD(&chan->notifiers);
304 list_add(&chan->head, &abi16->channels);
305
306 /* create channel object and initialise dma and fence management */
307 ret = nouveau_channel_new(drm, device, init->fb_ctxdma_handle,
308 init->tt_ctxdma_handle, &chan->chan);
309 if (ret)
310 goto done;
311
312 init->channel = chan->chan->chid;
313
314 if (device->info.family >= NV_DEVICE_INFO_V0_TESLA)
315 init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_VRAM |
316 NOUVEAU_GEM_DOMAIN_GART;
317 else
318 if (chan->chan->push.buffer->bo.mem.mem_type == TTM_PL_VRAM)
319 init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_VRAM;
320 else
321 init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_GART;
322
323 if (device->info.family < NV_DEVICE_INFO_V0_CELSIUS) {
324 init->subchan[0].handle = 0x00000000;
325 init->subchan[0].grclass = 0x0000;
326 init->subchan[1].handle = chan->chan->nvsw.handle;
327 init->subchan[1].grclass = 0x506e;
328 init->nr_subchan = 2;
329 }
330
331 /* Named memory object area */
332 ret = nouveau_gem_new(cli, PAGE_SIZE, 0, NOUVEAU_GEM_DOMAIN_GART,
333 0, 0, &chan->ntfy);
334 if (ret == 0)
335 ret = nouveau_bo_pin(chan->ntfy, TTM_PL_FLAG_TT, false);
336 if (ret)
337 goto done;
338
339 if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
340 ret = nouveau_vma_new(chan->ntfy, &cli->vmm, &chan->ntfy_vma);
341 if (ret)
342 goto done;
343 }
344
345 ret = drm_gem_handle_create(file_priv, &chan->ntfy->gem,
346 &init->notifier_handle);
347 if (ret)
348 goto done;
349
350 ret = nvkm_mm_init(&chan->heap, 0, 0, PAGE_SIZE, 1);
351 done:
352 if (ret)
353 nouveau_abi16_chan_fini(abi16, chan);
354 return nouveau_abi16_put(abi16, ret);
355 }
356
357 static struct nouveau_abi16_chan *
358 nouveau_abi16_chan(struct nouveau_abi16 *abi16, int channel)
359 {
360 struct nouveau_abi16_chan *chan;
361
362 list_for_each_entry(chan, &abi16->channels, head) {
363 if (chan->chan->chid == channel)
364 return chan;
365 }
366
367 return NULL;
368 }
369
370 int
371 nouveau_abi16_usif(struct drm_file *file_priv, void *data, u32 size)
372 {
373 union {
374 struct nvif_ioctl_v0 v0;
375 } *args = data;
376 struct nouveau_abi16_chan *chan;
377 struct nouveau_abi16 *abi16;
378 int ret = -ENOSYS;
379
380 if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, true))) {
381 switch (args->v0.type) {
382 case NVIF_IOCTL_V0_NEW:
383 case NVIF_IOCTL_V0_MTHD:
384 case NVIF_IOCTL_V0_SCLASS:
385 break;
386 default:
387 return -EACCES;
388 }
389 } else
390 return ret;
391
392 if (!(abi16 = nouveau_abi16(file_priv)))
393 return -ENOMEM;
394
395 if (args->v0.token != ~0ULL) {
396 if (!(chan = nouveau_abi16_chan(abi16, args->v0.token)))
397 return -EINVAL;
398 args->v0.object = nvif_handle(&chan->chan->user);
399 args->v0.owner = NVIF_IOCTL_V0_OWNER_ANY;
400 return 0;
401 }
402
403 args->v0.object = nvif_handle(&abi16->device.object);
404 args->v0.owner = NVIF_IOCTL_V0_OWNER_ANY;
405 return 0;
406 }
407
408 int
409 nouveau_abi16_ioctl_channel_free(ABI16_IOCTL_ARGS)
410 {
411 struct drm_nouveau_channel_free *req = data;
412 struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv);
413 struct nouveau_abi16_chan *chan;
414
415 if (unlikely(!abi16))
416 return -ENOMEM;
417
418 chan = nouveau_abi16_chan(abi16, req->channel);
419 if (!chan)
420 return nouveau_abi16_put(abi16, -ENOENT);
421 nouveau_abi16_chan_fini(abi16, chan);
422 return nouveau_abi16_put(abi16, 0);
423 }
424
425 int
426 nouveau_abi16_ioctl_grobj_alloc(ABI16_IOCTL_ARGS)
427 {
428 struct drm_nouveau_grobj_alloc *init = data;
429 struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv);
430 struct nouveau_abi16_chan *chan;
431 struct nouveau_abi16_ntfy *ntfy;
432 struct nvif_client *client;
433 struct nvif_sclass *sclass;
434 s32 oclass = 0;
435 int ret, i;
436
437 if (unlikely(!abi16))
438 return -ENOMEM;
439
440 if (init->handle == ~0)
441 return nouveau_abi16_put(abi16, -EINVAL);
442 client = abi16->device.object.client;
443
444 chan = nouveau_abi16_chan(abi16, init->channel);
445 if (!chan)
446 return nouveau_abi16_put(abi16, -ENOENT);
447
448 ret = nvif_object_sclass_get(&chan->chan->user, &sclass);
449 if (ret < 0)
450 return nouveau_abi16_put(abi16, ret);
451
452 if ((init->class & 0x00ff) == 0x006e) {
453 /* nvsw: compatibility with older 0x*6e class identifier */
454 for (i = 0; !oclass && i < ret; i++) {
455 switch (sclass[i].oclass) {
456 case NVIF_CLASS_SW_NV04:
457 case NVIF_CLASS_SW_NV10:
458 case NVIF_CLASS_SW_NV50:
459 case NVIF_CLASS_SW_GF100:
460 oclass = sclass[i].oclass;
461 break;
462 default:
463 break;
464 }
465 }
466 } else
467 if ((init->class & 0x00ff) == 0x00b1) {
468 /* msvld: compatibility with incorrect version exposure */
469 for (i = 0; i < ret; i++) {
470 if ((sclass[i].oclass & 0x00ff) == 0x00b1) {
471 oclass = sclass[i].oclass;
472 break;
473 }
474 }
475 } else
476 if ((init->class & 0x00ff) == 0x00b2) { /* mspdec */
477 /* mspdec: compatibility with incorrect version exposure */
478 for (i = 0; i < ret; i++) {
479 if ((sclass[i].oclass & 0x00ff) == 0x00b2) {
480 oclass = sclass[i].oclass;
481 break;
482 }
483 }
484 } else
485 if ((init->class & 0x00ff) == 0x00b3) { /* msppp */
486 /* msppp: compatibility with incorrect version exposure */
487 for (i = 0; i < ret; i++) {
488 if ((sclass[i].oclass & 0x00ff) == 0x00b3) {
489 oclass = sclass[i].oclass;
490 break;
491 }
492 }
493 } else {
494 oclass = init->class;
495 }
496
497 nvif_object_sclass_put(&sclass);
498 if (!oclass)
499 return nouveau_abi16_put(abi16, -EINVAL);
500
501 ntfy = kzalloc(sizeof(*ntfy), GFP_KERNEL);
502 if (!ntfy)
503 return nouveau_abi16_put(abi16, -ENOMEM);
504
505 list_add(&ntfy->head, &chan->notifiers);
506
507 client->route = NVDRM_OBJECT_ABI16;
508 ret = nvif_object_init(&chan->chan->user, init->handle, oclass,
509 NULL, 0, &ntfy->object);
510 client->route = NVDRM_OBJECT_NVIF;
511
512 if (ret)
513 nouveau_abi16_ntfy_fini(chan, ntfy);
514 return nouveau_abi16_put(abi16, ret);
515 }
516
517 int
518 nouveau_abi16_ioctl_notifierobj_alloc(ABI16_IOCTL_ARGS)
519 {
520 struct drm_nouveau_notifierobj_alloc *info = data;
521 struct nouveau_drm *drm = nouveau_drm(dev);
522 struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv);
523 struct nouveau_abi16_chan *chan;
524 struct nouveau_abi16_ntfy *ntfy;
525 struct nvif_device *device = &abi16->device;
526 struct nvif_client *client;
527 struct nv_dma_v0 args = {};
528 int ret;
529
530 if (unlikely(!abi16))
531 return -ENOMEM;
532
533 /* completely unnecessary for these chipsets... */
534 if (unlikely(device->info.family >= NV_DEVICE_INFO_V0_FERMI))
535 return nouveau_abi16_put(abi16, -EINVAL);
536 client = abi16->device.object.client;
537
538 chan = nouveau_abi16_chan(abi16, info->channel);
539 if (!chan)
540 return nouveau_abi16_put(abi16, -ENOENT);
541
542 ntfy = kzalloc(sizeof(*ntfy), GFP_KERNEL);
543 if (!ntfy)
544 return nouveau_abi16_put(abi16, -ENOMEM);
545
546 list_add(&ntfy->head, &chan->notifiers);
547
548 ret = nvkm_mm_head(&chan->heap, 0, 1, info->size, info->size, 1,
549 &ntfy->node);
550 if (ret)
551 goto done;
552
553 args.start = ntfy->node->offset;
554 args.limit = ntfy->node->offset + ntfy->node->length - 1;
555 if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
556 args.target = NV_DMA_V0_TARGET_VM;
557 args.access = NV_DMA_V0_ACCESS_VM;
558 args.start += chan->ntfy_vma->addr;
559 args.limit += chan->ntfy_vma->addr;
560 } else
561 if (drm->agp.bridge) {
562 args.target = NV_DMA_V0_TARGET_AGP;
563 args.access = NV_DMA_V0_ACCESS_RDWR;
564 args.start += drm->agp.base + chan->ntfy->bo.offset;
565 args.limit += drm->agp.base + chan->ntfy->bo.offset;
566 } else {
567 args.target = NV_DMA_V0_TARGET_VM;
568 args.access = NV_DMA_V0_ACCESS_RDWR;
569 args.start += chan->ntfy->bo.offset;
570 args.limit += chan->ntfy->bo.offset;
571 }
572
573 client->route = NVDRM_OBJECT_ABI16;
574 client->super = true;
575 ret = nvif_object_init(&chan->chan->user, info->handle,
576 NV_DMA_IN_MEMORY, &args, sizeof(args),
577 &ntfy->object);
578 client->super = false;
579 client->route = NVDRM_OBJECT_NVIF;
580 if (ret)
581 goto done;
582
583 info->offset = ntfy->node->offset;
584 done:
585 if (ret)
586 nouveau_abi16_ntfy_fini(chan, ntfy);
587 return nouveau_abi16_put(abi16, ret);
588 }
589
590 int
591 nouveau_abi16_ioctl_gpuobj_free(ABI16_IOCTL_ARGS)
592 {
593 struct drm_nouveau_gpuobj_free *fini = data;
594 struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv);
595 struct nouveau_abi16_chan *chan;
596 struct nouveau_abi16_ntfy *ntfy;
597 int ret = -ENOENT;
598
599 if (unlikely(!abi16))
600 return -ENOMEM;
601
602 chan = nouveau_abi16_chan(abi16, fini->channel);
603 if (!chan)
604 return nouveau_abi16_put(abi16, -EINVAL);
605
606 /* synchronize with the user channel and destroy the gpu object */
607 nouveau_channel_idle(chan->chan);
608
609 list_for_each_entry(ntfy, &chan->notifiers, head) {
610 if (ntfy->object.handle == fini->handle) {
611 nouveau_abi16_ntfy_fini(chan, ntfy);
612 ret = 0;
613 break;
614 }
615 }
616
617 return nouveau_abi16_put(abi16, ret);
618 }