Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / gpu / drm / nouveau / nouveau_abi16.c
blobece650a0c5f9fb01f6eb8d4c0033dc9602a9e330
1 /*
2 * Copyright 2012 Red Hat Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
24 #include <nvif/client.h>
25 #include <nvif/driver.h>
26 #include <nvif/ioctl.h>
27 #include <nvif/class.h>
28 #include <nvif/cl0002.h>
29 #include <nvif/cla06f.h>
30 #include <nvif/unpack.h>
32 #include "nouveau_drv.h"
33 #include "nouveau_dma.h"
34 #include "nouveau_gem.h"
35 #include "nouveau_chan.h"
36 #include "nouveau_abi16.h"
37 #include "nouveau_vmm.h"
39 static struct nouveau_abi16 *
40 nouveau_abi16(struct drm_file *file_priv)
42 struct nouveau_cli *cli = nouveau_cli(file_priv);
43 if (!cli->abi16) {
44 struct nouveau_abi16 *abi16;
45 cli->abi16 = abi16 = kzalloc(sizeof(*abi16), GFP_KERNEL);
46 if (cli->abi16) {
47 struct nv_device_v0 args = {
48 .device = ~0ULL,
51 INIT_LIST_HEAD(&abi16->channels);
53 /* allocate device object targeting client's default
54 * device (ie. the one that belongs to the fd it
55 * opened)
57 if (nvif_device_init(&cli->base.object, 0, NV_DEVICE,
58 &args, sizeof(args),
59 &abi16->device) == 0)
60 return cli->abi16;
62 kfree(cli->abi16);
63 cli->abi16 = NULL;
66 return cli->abi16;
69 struct nouveau_abi16 *
70 nouveau_abi16_get(struct drm_file *file_priv)
72 struct nouveau_cli *cli = nouveau_cli(file_priv);
73 mutex_lock(&cli->mutex);
74 if (nouveau_abi16(file_priv))
75 return cli->abi16;
76 mutex_unlock(&cli->mutex);
77 return NULL;
80 int
81 nouveau_abi16_put(struct nouveau_abi16 *abi16, int ret)
83 struct nouveau_cli *cli = (void *)abi16->device.object.client;
84 mutex_unlock(&cli->mutex);
85 return ret;
88 s32
89 nouveau_abi16_swclass(struct nouveau_drm *drm)
91 switch (drm->client.device.info.family) {
92 case NV_DEVICE_INFO_V0_TNT:
93 return NVIF_CLASS_SW_NV04;
94 case NV_DEVICE_INFO_V0_CELSIUS:
95 case NV_DEVICE_INFO_V0_KELVIN:
96 case NV_DEVICE_INFO_V0_RANKINE:
97 case NV_DEVICE_INFO_V0_CURIE:
98 return NVIF_CLASS_SW_NV10;
99 case NV_DEVICE_INFO_V0_TESLA:
100 return NVIF_CLASS_SW_NV50;
101 case NV_DEVICE_INFO_V0_FERMI:
102 case NV_DEVICE_INFO_V0_KEPLER:
103 case NV_DEVICE_INFO_V0_MAXWELL:
104 case NV_DEVICE_INFO_V0_PASCAL:
105 return NVIF_CLASS_SW_GF100;
108 return 0x0000;
111 static void
112 nouveau_abi16_ntfy_fini(struct nouveau_abi16_chan *chan,
113 struct nouveau_abi16_ntfy *ntfy)
115 nvif_object_fini(&ntfy->object);
116 nvkm_mm_free(&chan->heap, &ntfy->node);
117 list_del(&ntfy->head);
118 kfree(ntfy);
121 static void
122 nouveau_abi16_chan_fini(struct nouveau_abi16 *abi16,
123 struct nouveau_abi16_chan *chan)
125 struct nouveau_abi16_ntfy *ntfy, *temp;
127 /* wait for all activity to stop before releasing notify object, which
128 * may be still in use */
129 if (chan->chan && chan->ntfy)
130 nouveau_channel_idle(chan->chan);
132 /* cleanup notifier state */
133 list_for_each_entry_safe(ntfy, temp, &chan->notifiers, head) {
134 nouveau_abi16_ntfy_fini(chan, ntfy);
137 if (chan->ntfy) {
138 nouveau_vma_del(&chan->ntfy_vma);
139 nouveau_bo_unpin(chan->ntfy);
140 drm_gem_object_unreference_unlocked(&chan->ntfy->gem);
143 if (chan->heap.block_size)
144 nvkm_mm_fini(&chan->heap);
146 /* destroy channel object, all children will be killed too */
147 if (chan->chan) {
148 nouveau_channel_idle(chan->chan);
149 nouveau_channel_del(&chan->chan);
152 list_del(&chan->head);
153 kfree(chan);
156 void
157 nouveau_abi16_fini(struct nouveau_abi16 *abi16)
159 struct nouveau_cli *cli = (void *)abi16->device.object.client;
160 struct nouveau_abi16_chan *chan, *temp;
162 /* cleanup channels */
163 list_for_each_entry_safe(chan, temp, &abi16->channels, head) {
164 nouveau_abi16_chan_fini(abi16, chan);
167 /* destroy the device object */
168 nvif_device_fini(&abi16->device);
170 kfree(cli->abi16);
171 cli->abi16 = NULL;
175 nouveau_abi16_ioctl_getparam(ABI16_IOCTL_ARGS)
177 struct nouveau_cli *cli = nouveau_cli(file_priv);
178 struct nouveau_drm *drm = nouveau_drm(dev);
179 struct nvif_device *device = &drm->client.device;
180 struct nvkm_gr *gr = nvxx_gr(device);
181 struct drm_nouveau_getparam *getparam = data;
183 switch (getparam->param) {
184 case NOUVEAU_GETPARAM_CHIPSET_ID:
185 getparam->value = device->info.chipset;
186 break;
187 case NOUVEAU_GETPARAM_PCI_VENDOR:
188 if (device->info.platform != NV_DEVICE_INFO_V0_SOC)
189 getparam->value = dev->pdev->vendor;
190 else
191 getparam->value = 0;
192 break;
193 case NOUVEAU_GETPARAM_PCI_DEVICE:
194 if (device->info.platform != NV_DEVICE_INFO_V0_SOC)
195 getparam->value = dev->pdev->device;
196 else
197 getparam->value = 0;
198 break;
199 case NOUVEAU_GETPARAM_BUS_TYPE:
200 switch (device->info.platform) {
201 case NV_DEVICE_INFO_V0_AGP : getparam->value = 0; break;
202 case NV_DEVICE_INFO_V0_PCI : getparam->value = 1; break;
203 case NV_DEVICE_INFO_V0_PCIE: getparam->value = 2; break;
204 case NV_DEVICE_INFO_V0_SOC : getparam->value = 3; break;
205 case NV_DEVICE_INFO_V0_IGP :
206 if (!pci_is_pcie(dev->pdev))
207 getparam->value = 1;
208 else
209 getparam->value = 2;
210 break;
211 default:
212 WARN_ON(1);
213 break;
215 case NOUVEAU_GETPARAM_FB_SIZE:
216 getparam->value = drm->gem.vram_available;
217 break;
218 case NOUVEAU_GETPARAM_AGP_SIZE:
219 getparam->value = drm->gem.gart_available;
220 break;
221 case NOUVEAU_GETPARAM_VM_VRAM_BASE:
222 getparam->value = 0; /* deprecated */
223 break;
224 case NOUVEAU_GETPARAM_PTIMER_TIME:
225 getparam->value = nvif_device_time(device);
226 break;
227 case NOUVEAU_GETPARAM_HAS_BO_USAGE:
228 getparam->value = 1;
229 break;
230 case NOUVEAU_GETPARAM_HAS_PAGEFLIP:
231 getparam->value = 1;
232 break;
233 case NOUVEAU_GETPARAM_GRAPH_UNITS:
234 getparam->value = nvkm_gr_units(gr);
235 break;
236 default:
237 NV_PRINTK(dbg, cli, "unknown parameter %lld\n", getparam->param);
238 return -EINVAL;
241 return 0;
245 nouveau_abi16_ioctl_setparam(ABI16_IOCTL_ARGS)
247 return -EINVAL;
251 nouveau_abi16_ioctl_channel_alloc(ABI16_IOCTL_ARGS)
253 struct drm_nouveau_channel_alloc *init = data;
254 struct nouveau_cli *cli = nouveau_cli(file_priv);
255 struct nouveau_drm *drm = nouveau_drm(dev);
256 struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv);
257 struct nouveau_abi16_chan *chan;
258 struct nvif_device *device;
259 int ret;
261 if (unlikely(!abi16))
262 return -ENOMEM;
264 if (!drm->channel)
265 return nouveau_abi16_put(abi16, -ENODEV);
267 device = &abi16->device;
269 /* hack to allow channel engine type specification on kepler */
270 if (device->info.family >= NV_DEVICE_INFO_V0_KEPLER) {
271 if (init->fb_ctxdma_handle != ~0)
272 init->fb_ctxdma_handle = NVA06F_V0_ENGINE_GR;
273 else {
274 init->fb_ctxdma_handle = 0;
275 #define _(A,B) if (init->tt_ctxdma_handle & (A)) init->fb_ctxdma_handle |= (B)
276 _(0x01, NVA06F_V0_ENGINE_GR);
277 _(0x02, NVA06F_V0_ENGINE_MSPDEC);
278 _(0x04, NVA06F_V0_ENGINE_MSPPP);
279 _(0x08, NVA06F_V0_ENGINE_MSVLD);
280 _(0x10, NVA06F_V0_ENGINE_CE0);
281 _(0x20, NVA06F_V0_ENGINE_CE1);
282 _(0x40, NVA06F_V0_ENGINE_MSENC);
283 #undef _
286 /* allow flips to be executed if this is a graphics channel */
287 init->tt_ctxdma_handle = 0;
288 if (init->fb_ctxdma_handle == NVA06F_V0_ENGINE_GR)
289 init->tt_ctxdma_handle = 1;
292 if (init->fb_ctxdma_handle == ~0 || init->tt_ctxdma_handle == ~0)
293 return nouveau_abi16_put(abi16, -EINVAL);
295 /* allocate "abi16 channel" data and make up a handle for it */
296 chan = kzalloc(sizeof(*chan), GFP_KERNEL);
297 if (!chan)
298 return nouveau_abi16_put(abi16, -ENOMEM);
300 INIT_LIST_HEAD(&chan->notifiers);
301 list_add(&chan->head, &abi16->channels);
303 /* create channel object and initialise dma and fence management */
304 ret = nouveau_channel_new(drm, device, init->fb_ctxdma_handle,
305 init->tt_ctxdma_handle, &chan->chan);
306 if (ret)
307 goto done;
309 init->channel = chan->chan->chid;
311 if (device->info.family >= NV_DEVICE_INFO_V0_TESLA)
312 init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_VRAM |
313 NOUVEAU_GEM_DOMAIN_GART;
314 else
315 if (chan->chan->push.buffer->bo.mem.mem_type == TTM_PL_VRAM)
316 init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_VRAM;
317 else
318 init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_GART;
320 if (device->info.family < NV_DEVICE_INFO_V0_CELSIUS) {
321 init->subchan[0].handle = 0x00000000;
322 init->subchan[0].grclass = 0x0000;
323 init->subchan[1].handle = chan->chan->nvsw.handle;
324 init->subchan[1].grclass = 0x506e;
325 init->nr_subchan = 2;
328 /* Named memory object area */
329 ret = nouveau_gem_new(cli, PAGE_SIZE, 0, NOUVEAU_GEM_DOMAIN_GART,
330 0, 0, &chan->ntfy);
331 if (ret == 0)
332 ret = nouveau_bo_pin(chan->ntfy, TTM_PL_FLAG_TT, false);
333 if (ret)
334 goto done;
336 if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
337 ret = nouveau_vma_new(chan->ntfy, &cli->vmm, &chan->ntfy_vma);
338 if (ret)
339 goto done;
342 ret = drm_gem_handle_create(file_priv, &chan->ntfy->gem,
343 &init->notifier_handle);
344 if (ret)
345 goto done;
347 ret = nvkm_mm_init(&chan->heap, 0, 0, PAGE_SIZE, 1);
348 done:
349 if (ret)
350 nouveau_abi16_chan_fini(abi16, chan);
351 return nouveau_abi16_put(abi16, ret);
354 static struct nouveau_abi16_chan *
355 nouveau_abi16_chan(struct nouveau_abi16 *abi16, int channel)
357 struct nouveau_abi16_chan *chan;
359 list_for_each_entry(chan, &abi16->channels, head) {
360 if (chan->chan->chid == channel)
361 return chan;
364 return NULL;
368 nouveau_abi16_usif(struct drm_file *file_priv, void *data, u32 size)
370 union {
371 struct nvif_ioctl_v0 v0;
372 } *args = data;
373 struct nouveau_abi16_chan *chan;
374 struct nouveau_abi16 *abi16;
375 int ret = -ENOSYS;
377 if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, true))) {
378 switch (args->v0.type) {
379 case NVIF_IOCTL_V0_NEW:
380 case NVIF_IOCTL_V0_MTHD:
381 case NVIF_IOCTL_V0_SCLASS:
382 break;
383 default:
384 return -EACCES;
386 } else
387 return ret;
389 if (!(abi16 = nouveau_abi16(file_priv)))
390 return -ENOMEM;
392 if (args->v0.token != ~0ULL) {
393 if (!(chan = nouveau_abi16_chan(abi16, args->v0.token)))
394 return -EINVAL;
395 args->v0.object = nvif_handle(&chan->chan->user);
396 args->v0.owner = NVIF_IOCTL_V0_OWNER_ANY;
397 return 0;
400 args->v0.object = nvif_handle(&abi16->device.object);
401 args->v0.owner = NVIF_IOCTL_V0_OWNER_ANY;
402 return 0;
406 nouveau_abi16_ioctl_channel_free(ABI16_IOCTL_ARGS)
408 struct drm_nouveau_channel_free *req = data;
409 struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv);
410 struct nouveau_abi16_chan *chan;
412 if (unlikely(!abi16))
413 return -ENOMEM;
415 chan = nouveau_abi16_chan(abi16, req->channel);
416 if (!chan)
417 return nouveau_abi16_put(abi16, -ENOENT);
418 nouveau_abi16_chan_fini(abi16, chan);
419 return nouveau_abi16_put(abi16, 0);
423 nouveau_abi16_ioctl_grobj_alloc(ABI16_IOCTL_ARGS)
425 struct drm_nouveau_grobj_alloc *init = data;
426 struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv);
427 struct nouveau_abi16_chan *chan;
428 struct nouveau_abi16_ntfy *ntfy;
429 struct nvif_client *client;
430 struct nvif_sclass *sclass;
431 s32 oclass = 0;
432 int ret, i;
434 if (unlikely(!abi16))
435 return -ENOMEM;
437 if (init->handle == ~0)
438 return nouveau_abi16_put(abi16, -EINVAL);
439 client = abi16->device.object.client;
441 chan = nouveau_abi16_chan(abi16, init->channel);
442 if (!chan)
443 return nouveau_abi16_put(abi16, -ENOENT);
445 ret = nvif_object_sclass_get(&chan->chan->user, &sclass);
446 if (ret < 0)
447 return nouveau_abi16_put(abi16, ret);
449 if ((init->class & 0x00ff) == 0x006e) {
450 /* nvsw: compatibility with older 0x*6e class identifier */
451 for (i = 0; !oclass && i < ret; i++) {
452 switch (sclass[i].oclass) {
453 case NVIF_CLASS_SW_NV04:
454 case NVIF_CLASS_SW_NV10:
455 case NVIF_CLASS_SW_NV50:
456 case NVIF_CLASS_SW_GF100:
457 oclass = sclass[i].oclass;
458 break;
459 default:
460 break;
463 } else
464 if ((init->class & 0x00ff) == 0x00b1) {
465 /* msvld: compatibility with incorrect version exposure */
466 for (i = 0; i < ret; i++) {
467 if ((sclass[i].oclass & 0x00ff) == 0x00b1) {
468 oclass = sclass[i].oclass;
469 break;
472 } else
473 if ((init->class & 0x00ff) == 0x00b2) { /* mspdec */
474 /* mspdec: compatibility with incorrect version exposure */
475 for (i = 0; i < ret; i++) {
476 if ((sclass[i].oclass & 0x00ff) == 0x00b2) {
477 oclass = sclass[i].oclass;
478 break;
481 } else
482 if ((init->class & 0x00ff) == 0x00b3) { /* msppp */
483 /* msppp: compatibility with incorrect version exposure */
484 for (i = 0; i < ret; i++) {
485 if ((sclass[i].oclass & 0x00ff) == 0x00b3) {
486 oclass = sclass[i].oclass;
487 break;
490 } else {
491 oclass = init->class;
494 nvif_object_sclass_put(&sclass);
495 if (!oclass)
496 return nouveau_abi16_put(abi16, -EINVAL);
498 ntfy = kzalloc(sizeof(*ntfy), GFP_KERNEL);
499 if (!ntfy)
500 return nouveau_abi16_put(abi16, -ENOMEM);
502 list_add(&ntfy->head, &chan->notifiers);
504 client->route = NVDRM_OBJECT_ABI16;
505 ret = nvif_object_init(&chan->chan->user, init->handle, oclass,
506 NULL, 0, &ntfy->object);
507 client->route = NVDRM_OBJECT_NVIF;
509 if (ret)
510 nouveau_abi16_ntfy_fini(chan, ntfy);
511 return nouveau_abi16_put(abi16, ret);
515 nouveau_abi16_ioctl_notifierobj_alloc(ABI16_IOCTL_ARGS)
517 struct drm_nouveau_notifierobj_alloc *info = data;
518 struct nouveau_drm *drm = nouveau_drm(dev);
519 struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv);
520 struct nouveau_abi16_chan *chan;
521 struct nouveau_abi16_ntfy *ntfy;
522 struct nvif_device *device = &abi16->device;
523 struct nvif_client *client;
524 struct nv_dma_v0 args = {};
525 int ret;
527 if (unlikely(!abi16))
528 return -ENOMEM;
530 /* completely unnecessary for these chipsets... */
531 if (unlikely(device->info.family >= NV_DEVICE_INFO_V0_FERMI))
532 return nouveau_abi16_put(abi16, -EINVAL);
533 client = abi16->device.object.client;
535 chan = nouveau_abi16_chan(abi16, info->channel);
536 if (!chan)
537 return nouveau_abi16_put(abi16, -ENOENT);
539 ntfy = kzalloc(sizeof(*ntfy), GFP_KERNEL);
540 if (!ntfy)
541 return nouveau_abi16_put(abi16, -ENOMEM);
543 list_add(&ntfy->head, &chan->notifiers);
545 ret = nvkm_mm_head(&chan->heap, 0, 1, info->size, info->size, 1,
546 &ntfy->node);
547 if (ret)
548 goto done;
550 args.start = ntfy->node->offset;
551 args.limit = ntfy->node->offset + ntfy->node->length - 1;
552 if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
553 args.target = NV_DMA_V0_TARGET_VM;
554 args.access = NV_DMA_V0_ACCESS_VM;
555 args.start += chan->ntfy_vma->addr;
556 args.limit += chan->ntfy_vma->addr;
557 } else
558 if (drm->agp.bridge) {
559 args.target = NV_DMA_V0_TARGET_AGP;
560 args.access = NV_DMA_V0_ACCESS_RDWR;
561 args.start += drm->agp.base + chan->ntfy->bo.offset;
562 args.limit += drm->agp.base + chan->ntfy->bo.offset;
563 } else {
564 args.target = NV_DMA_V0_TARGET_VM;
565 args.access = NV_DMA_V0_ACCESS_RDWR;
566 args.start += chan->ntfy->bo.offset;
567 args.limit += chan->ntfy->bo.offset;
570 client->route = NVDRM_OBJECT_ABI16;
571 client->super = true;
572 ret = nvif_object_init(&chan->chan->user, info->handle,
573 NV_DMA_IN_MEMORY, &args, sizeof(args),
574 &ntfy->object);
575 client->super = false;
576 client->route = NVDRM_OBJECT_NVIF;
577 if (ret)
578 goto done;
580 info->offset = ntfy->node->offset;
581 done:
582 if (ret)
583 nouveau_abi16_ntfy_fini(chan, ntfy);
584 return nouveau_abi16_put(abi16, ret);
588 nouveau_abi16_ioctl_gpuobj_free(ABI16_IOCTL_ARGS)
590 struct drm_nouveau_gpuobj_free *fini = data;
591 struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv);
592 struct nouveau_abi16_chan *chan;
593 struct nouveau_abi16_ntfy *ntfy;
594 int ret = -ENOENT;
596 if (unlikely(!abi16))
597 return -ENOMEM;
599 chan = nouveau_abi16_chan(abi16, fini->channel);
600 if (!chan)
601 return nouveau_abi16_put(abi16, -EINVAL);
603 /* synchronize with the user channel and destroy the gpu object */
604 nouveau_channel_idle(chan->chan);
606 list_for_each_entry(ntfy, &chan->notifiers, head) {
607 if (ntfy->object.handle == fini->handle) {
608 nouveau_abi16_ntfy_fini(chan, ntfy);
609 ret = 0;
610 break;
614 return nouveau_abi16_put(abi16, ret);