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/fifo.h>
27 #include <nvif/ioctl.h>
28 #include <nvif/class.h>
29 #include <nvif/cl0002.h>
30 #include <nvif/unpack.h>
32 #include "nouveau_drv.h"
33 #include "nouveau_dma.h"
34 #include "nouveau_exec.h"
35 #include "nouveau_gem.h"
36 #include "nouveau_chan.h"
37 #include "nouveau_abi16.h"
38 #include "nouveau_vmm.h"
39 #include "nouveau_sched.h"
41 static struct nouveau_abi16 *
42 nouveau_abi16(struct drm_file *file_priv)
44 struct nouveau_cli *cli = nouveau_cli(file_priv);
46 struct nouveau_abi16 *abi16;
47 cli->abi16 = abi16 = kzalloc(sizeof(*abi16), GFP_KERNEL);
49 struct nv_device_v0 args = {
53 INIT_LIST_HEAD(&abi16->channels);
55 /* allocate device object targeting client's default
56 * device (ie. the one that belongs to the fd it
59 if (nvif_device_ctor(&cli->base.object, "abi16Device",
60 0, NV_DEVICE, &args, sizeof(args),
71 struct nouveau_abi16 *
72 nouveau_abi16_get(struct drm_file *file_priv)
74 struct nouveau_cli *cli = nouveau_cli(file_priv);
75 mutex_lock(&cli->mutex);
76 if (nouveau_abi16(file_priv))
78 mutex_unlock(&cli->mutex);
83 nouveau_abi16_put(struct nouveau_abi16 *abi16, int ret)
85 struct nouveau_cli *cli = (void *)abi16->device.object.client;
86 mutex_unlock(&cli->mutex);
91 nouveau_abi16_swclass(struct nouveau_drm *drm)
93 switch (drm->client.device.info.family) {
94 case NV_DEVICE_INFO_V0_TNT:
95 return NVIF_CLASS_SW_NV04;
96 case NV_DEVICE_INFO_V0_CELSIUS:
97 case NV_DEVICE_INFO_V0_KELVIN:
98 case NV_DEVICE_INFO_V0_RANKINE:
99 case NV_DEVICE_INFO_V0_CURIE:
100 return NVIF_CLASS_SW_NV10;
101 case NV_DEVICE_INFO_V0_TESLA:
102 return NVIF_CLASS_SW_NV50;
103 case NV_DEVICE_INFO_V0_FERMI:
104 case NV_DEVICE_INFO_V0_KEPLER:
105 case NV_DEVICE_INFO_V0_MAXWELL:
106 case NV_DEVICE_INFO_V0_PASCAL:
107 case NV_DEVICE_INFO_V0_VOLTA:
108 return NVIF_CLASS_SW_GF100;
115 nouveau_abi16_ntfy_fini(struct nouveau_abi16_chan *chan,
116 struct nouveau_abi16_ntfy *ntfy)
118 nvif_object_dtor(&ntfy->object);
119 nvkm_mm_free(&chan->heap, &ntfy->node);
120 list_del(&ntfy->head);
125 nouveau_abi16_chan_fini(struct nouveau_abi16 *abi16,
126 struct nouveau_abi16_chan *chan)
128 struct nouveau_abi16_ntfy *ntfy, *temp;
130 /* Cancel all jobs from the entity's queue. */
132 drm_sched_entity_fini(&chan->sched->entity);
135 nouveau_channel_idle(chan->chan);
138 nouveau_sched_destroy(&chan->sched);
140 /* cleanup notifier state */
141 list_for_each_entry_safe(ntfy, temp, &chan->notifiers, head) {
142 nouveau_abi16_ntfy_fini(chan, ntfy);
146 nouveau_vma_del(&chan->ntfy_vma);
147 nouveau_bo_unpin(chan->ntfy);
148 drm_gem_object_put(&chan->ntfy->bo.base);
151 if (chan->heap.block_size)
152 nvkm_mm_fini(&chan->heap);
154 /* destroy channel object, all children will be killed too */
156 nvif_object_dtor(&chan->ce);
157 nouveau_channel_del(&chan->chan);
160 list_del(&chan->head);
165 nouveau_abi16_fini(struct nouveau_abi16 *abi16)
167 struct nouveau_cli *cli = (void *)abi16->device.object.client;
168 struct nouveau_abi16_chan *chan, *temp;
170 /* cleanup channels */
171 list_for_each_entry_safe(chan, temp, &abi16->channels, head) {
172 nouveau_abi16_chan_fini(abi16, chan);
175 /* destroy the device object */
176 nvif_device_dtor(&abi16->device);
183 getparam_dma_ib_max(struct nvif_device *device)
185 const struct nvif_mclass dmas[] = {
186 { NV03_CHANNEL_DMA, 0 },
187 { NV10_CHANNEL_DMA, 0 },
188 { NV17_CHANNEL_DMA, 0 },
189 { NV40_CHANNEL_DMA, 0 },
193 return nvif_mclass(&device->object, dmas) < 0 ? NV50_DMA_IB_MAX : 0;
197 nouveau_abi16_ioctl_getparam(ABI16_IOCTL_ARGS)
199 struct nouveau_cli *cli = nouveau_cli(file_priv);
200 struct nouveau_drm *drm = nouveau_drm(dev);
201 struct nvif_device *device = &drm->client.device;
202 struct nvkm_device *nvkm_device = nvxx_device(&drm->client.device);
203 struct nvkm_gr *gr = nvxx_gr(device);
204 struct drm_nouveau_getparam *getparam = data;
205 struct pci_dev *pdev = to_pci_dev(dev->dev);
207 switch (getparam->param) {
208 case NOUVEAU_GETPARAM_CHIPSET_ID:
209 getparam->value = device->info.chipset;
211 case NOUVEAU_GETPARAM_PCI_VENDOR:
212 if (device->info.platform != NV_DEVICE_INFO_V0_SOC)
213 getparam->value = pdev->vendor;
217 case NOUVEAU_GETPARAM_PCI_DEVICE:
218 if (device->info.platform != NV_DEVICE_INFO_V0_SOC)
219 getparam->value = pdev->device;
223 case NOUVEAU_GETPARAM_BUS_TYPE:
224 switch (device->info.platform) {
225 case NV_DEVICE_INFO_V0_AGP : getparam->value = 0; break;
226 case NV_DEVICE_INFO_V0_PCI : getparam->value = 1; break;
227 case NV_DEVICE_INFO_V0_PCIE: getparam->value = 2; break;
228 case NV_DEVICE_INFO_V0_SOC : getparam->value = 3; break;
229 case NV_DEVICE_INFO_V0_IGP :
230 if (!pci_is_pcie(pdev))
240 case NOUVEAU_GETPARAM_FB_SIZE:
241 getparam->value = drm->gem.vram_available;
243 case NOUVEAU_GETPARAM_AGP_SIZE:
244 getparam->value = drm->gem.gart_available;
246 case NOUVEAU_GETPARAM_VM_VRAM_BASE:
247 getparam->value = 0; /* deprecated */
249 case NOUVEAU_GETPARAM_PTIMER_TIME:
250 getparam->value = nvif_device_time(device);
252 case NOUVEAU_GETPARAM_HAS_BO_USAGE:
255 case NOUVEAU_GETPARAM_HAS_PAGEFLIP:
258 case NOUVEAU_GETPARAM_GRAPH_UNITS:
259 getparam->value = nvkm_gr_units(gr);
261 case NOUVEAU_GETPARAM_EXEC_PUSH_MAX: {
262 int ib_max = getparam_dma_ib_max(device);
264 getparam->value = nouveau_exec_push_max_from_ib_max(ib_max);
267 case NOUVEAU_GETPARAM_VRAM_BAR_SIZE:
268 getparam->value = nvkm_device->func->resource_size(nvkm_device, 1);
270 case NOUVEAU_GETPARAM_VRAM_USED: {
271 struct ttm_resource_manager *vram_mgr = ttm_manager_type(&drm->ttm.bdev, TTM_PL_VRAM);
272 getparam->value = (u64)ttm_resource_manager_usage(vram_mgr);
276 NV_PRINTK(dbg, cli, "unknown parameter %lld\n", getparam->param);
284 nouveau_abi16_ioctl_channel_alloc(ABI16_IOCTL_ARGS)
286 struct drm_nouveau_channel_alloc *init = data;
287 struct nouveau_cli *cli = nouveau_cli(file_priv);
288 struct nouveau_drm *drm = nouveau_drm(dev);
289 struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv);
290 struct nouveau_abi16_chan *chan;
291 struct nvif_device *device;
295 if (unlikely(!abi16))
299 return nouveau_abi16_put(abi16, -ENODEV);
301 /* If uvmm wasn't initialized until now disable it completely to prevent
302 * userspace from mixing up UAPIs.
304 * The client lock is already acquired by nouveau_abi16_get().
306 __nouveau_cli_disable_uvmm_noinit(cli);
308 device = &abi16->device;
309 engine = NV_DEVICE_HOST_RUNLIST_ENGINES_GR;
311 /* hack to allow channel engine type specification on kepler */
312 if (device->info.family >= NV_DEVICE_INFO_V0_KEPLER) {
313 if (init->fb_ctxdma_handle == ~0) {
314 switch (init->tt_ctxdma_handle) {
315 case 0x01: engine = NV_DEVICE_HOST_RUNLIST_ENGINES_GR ; break;
316 case 0x02: engine = NV_DEVICE_HOST_RUNLIST_ENGINES_MSPDEC; break;
317 case 0x04: engine = NV_DEVICE_HOST_RUNLIST_ENGINES_MSPPP ; break;
318 case 0x08: engine = NV_DEVICE_HOST_RUNLIST_ENGINES_MSVLD ; break;
319 case 0x30: engine = NV_DEVICE_HOST_RUNLIST_ENGINES_CE ; break;
321 return nouveau_abi16_put(abi16, -ENOSYS);
324 init->fb_ctxdma_handle = 0;
325 init->tt_ctxdma_handle = 0;
329 if (engine != NV_DEVICE_HOST_RUNLIST_ENGINES_CE)
330 runm = nvif_fifo_runlist(device, engine);
332 runm = nvif_fifo_runlist_ce(device);
334 if (!runm || init->fb_ctxdma_handle == ~0 || init->tt_ctxdma_handle == ~0)
335 return nouveau_abi16_put(abi16, -EINVAL);
337 /* allocate "abi16 channel" data and make up a handle for it */
338 chan = kzalloc(sizeof(*chan), GFP_KERNEL);
340 return nouveau_abi16_put(abi16, -ENOMEM);
342 INIT_LIST_HEAD(&chan->notifiers);
343 list_add(&chan->head, &abi16->channels);
345 /* create channel object and initialise dma and fence management */
346 ret = nouveau_channel_new(drm, device, false, runm, init->fb_ctxdma_handle,
347 init->tt_ctxdma_handle, &chan->chan);
351 /* If we're not using the VM_BIND uAPI, we don't need a scheduler.
353 * The client lock is already acquired by nouveau_abi16_get().
355 if (nouveau_cli_uvmm(cli)) {
356 ret = nouveau_sched_create(&chan->sched, drm, drm->sched_wq,
357 chan->chan->dma.ib_max);
362 init->channel = chan->chan->chid;
364 if (device->info.family >= NV_DEVICE_INFO_V0_TESLA)
365 init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_VRAM |
366 NOUVEAU_GEM_DOMAIN_GART;
368 if (chan->chan->push.buffer->bo.resource->mem_type == TTM_PL_VRAM)
369 init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_VRAM;
371 init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_GART;
373 if (device->info.family < NV_DEVICE_INFO_V0_CELSIUS) {
374 init->subchan[0].handle = 0x00000000;
375 init->subchan[0].grclass = 0x0000;
376 init->subchan[1].handle = chan->chan->nvsw.handle;
377 init->subchan[1].grclass = 0x506e;
378 init->nr_subchan = 2;
381 /* Workaround "nvc0" gallium driver using classes it doesn't allocate on
382 * Kepler and above. NVKM no longer always sets CE_CTX_VALID as part of
383 * channel init, now we know what that stuff actually is.
385 * Doesn't matter for Kepler/Pascal, CE context stored in NV_RAMIN.
387 * Userspace was fixed prior to adding Ampere support.
389 switch (device->info.family) {
390 case NV_DEVICE_INFO_V0_VOLTA:
391 ret = nvif_object_ctor(&chan->chan->user, "abi16CeWar", 0, VOLTA_DMA_COPY_A,
396 case NV_DEVICE_INFO_V0_TURING:
397 ret = nvif_object_ctor(&chan->chan->user, "abi16CeWar", 0, TURING_DMA_COPY_A,
406 /* Named memory object area */
407 ret = nouveau_gem_new(cli, PAGE_SIZE, 0, NOUVEAU_GEM_DOMAIN_GART,
410 ret = nouveau_bo_pin(chan->ntfy, NOUVEAU_GEM_DOMAIN_GART,
415 if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
416 ret = nouveau_vma_new(chan->ntfy, chan->chan->vmm,
422 ret = drm_gem_handle_create(file_priv, &chan->ntfy->bo.base,
423 &init->notifier_handle);
427 ret = nvkm_mm_init(&chan->heap, 0, 0, PAGE_SIZE, 1);
430 nouveau_abi16_chan_fini(abi16, chan);
431 return nouveau_abi16_put(abi16, ret);
434 static struct nouveau_abi16_chan *
435 nouveau_abi16_chan(struct nouveau_abi16 *abi16, int channel)
437 struct nouveau_abi16_chan *chan;
439 list_for_each_entry(chan, &abi16->channels, head) {
440 if (chan->chan->chid == channel)
448 nouveau_abi16_usif(struct drm_file *file_priv, void *data, u32 size)
451 struct nvif_ioctl_v0 v0;
453 struct nouveau_abi16_chan *chan;
454 struct nouveau_abi16 *abi16;
457 if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, true))) {
458 switch (args->v0.type) {
459 case NVIF_IOCTL_V0_NEW:
460 case NVIF_IOCTL_V0_MTHD:
461 case NVIF_IOCTL_V0_SCLASS:
469 if (!(abi16 = nouveau_abi16(file_priv)))
472 if (args->v0.token != ~0ULL) {
473 if (!(chan = nouveau_abi16_chan(abi16, args->v0.token)))
475 args->v0.object = nvif_handle(&chan->chan->user);
476 args->v0.owner = NVIF_IOCTL_V0_OWNER_ANY;
480 args->v0.object = nvif_handle(&abi16->device.object);
481 args->v0.owner = NVIF_IOCTL_V0_OWNER_ANY;
486 nouveau_abi16_ioctl_channel_free(ABI16_IOCTL_ARGS)
488 struct drm_nouveau_channel_free *req = data;
489 struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv);
490 struct nouveau_abi16_chan *chan;
492 if (unlikely(!abi16))
495 chan = nouveau_abi16_chan(abi16, req->channel);
497 return nouveau_abi16_put(abi16, -ENOENT);
498 nouveau_abi16_chan_fini(abi16, chan);
499 return nouveau_abi16_put(abi16, 0);
503 nouveau_abi16_ioctl_grobj_alloc(ABI16_IOCTL_ARGS)
505 struct drm_nouveau_grobj_alloc *init = data;
506 struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv);
507 struct nouveau_abi16_chan *chan;
508 struct nouveau_abi16_ntfy *ntfy;
509 struct nvif_client *client;
510 struct nvif_sclass *sclass;
514 if (unlikely(!abi16))
517 if (init->handle == ~0)
518 return nouveau_abi16_put(abi16, -EINVAL);
519 client = abi16->device.object.client;
521 chan = nouveau_abi16_chan(abi16, init->channel);
523 return nouveau_abi16_put(abi16, -ENOENT);
525 ret = nvif_object_sclass_get(&chan->chan->user, &sclass);
527 return nouveau_abi16_put(abi16, ret);
529 if ((init->class & 0x00ff) == 0x006e) {
530 /* nvsw: compatibility with older 0x*6e class identifier */
531 for (i = 0; !oclass && i < ret; i++) {
532 switch (sclass[i].oclass) {
533 case NVIF_CLASS_SW_NV04:
534 case NVIF_CLASS_SW_NV10:
535 case NVIF_CLASS_SW_NV50:
536 case NVIF_CLASS_SW_GF100:
537 oclass = sclass[i].oclass;
544 if ((init->class & 0x00ff) == 0x00b1) {
545 /* msvld: compatibility with incorrect version exposure */
546 for (i = 0; i < ret; i++) {
547 if ((sclass[i].oclass & 0x00ff) == 0x00b1) {
548 oclass = sclass[i].oclass;
553 if ((init->class & 0x00ff) == 0x00b2) { /* mspdec */
554 /* mspdec: compatibility with incorrect version exposure */
555 for (i = 0; i < ret; i++) {
556 if ((sclass[i].oclass & 0x00ff) == 0x00b2) {
557 oclass = sclass[i].oclass;
562 if ((init->class & 0x00ff) == 0x00b3) { /* msppp */
563 /* msppp: compatibility with incorrect version exposure */
564 for (i = 0; i < ret; i++) {
565 if ((sclass[i].oclass & 0x00ff) == 0x00b3) {
566 oclass = sclass[i].oclass;
571 oclass = init->class;
574 nvif_object_sclass_put(&sclass);
576 return nouveau_abi16_put(abi16, -EINVAL);
578 ntfy = kzalloc(sizeof(*ntfy), GFP_KERNEL);
580 return nouveau_abi16_put(abi16, -ENOMEM);
582 list_add(&ntfy->head, &chan->notifiers);
584 client->route = NVDRM_OBJECT_ABI16;
585 ret = nvif_object_ctor(&chan->chan->user, "abi16EngObj", init->handle,
586 oclass, NULL, 0, &ntfy->object);
587 client->route = NVDRM_OBJECT_NVIF;
590 nouveau_abi16_ntfy_fini(chan, ntfy);
591 return nouveau_abi16_put(abi16, ret);
595 nouveau_abi16_ioctl_notifierobj_alloc(ABI16_IOCTL_ARGS)
597 struct drm_nouveau_notifierobj_alloc *info = data;
598 struct nouveau_drm *drm = nouveau_drm(dev);
599 struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv);
600 struct nouveau_abi16_chan *chan;
601 struct nouveau_abi16_ntfy *ntfy;
602 struct nvif_device *device = &abi16->device;
603 struct nvif_client *client;
604 struct nv_dma_v0 args = {};
607 if (unlikely(!abi16))
610 /* completely unnecessary for these chipsets... */
611 if (unlikely(device->info.family >= NV_DEVICE_INFO_V0_FERMI))
612 return nouveau_abi16_put(abi16, -EINVAL);
613 client = abi16->device.object.client;
615 chan = nouveau_abi16_chan(abi16, info->channel);
617 return nouveau_abi16_put(abi16, -ENOENT);
619 ntfy = kzalloc(sizeof(*ntfy), GFP_KERNEL);
621 return nouveau_abi16_put(abi16, -ENOMEM);
623 list_add(&ntfy->head, &chan->notifiers);
625 ret = nvkm_mm_head(&chan->heap, 0, 1, info->size, info->size, 1,
630 args.start = ntfy->node->offset;
631 args.limit = ntfy->node->offset + ntfy->node->length - 1;
632 if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
633 args.target = NV_DMA_V0_TARGET_VM;
634 args.access = NV_DMA_V0_ACCESS_VM;
635 args.start += chan->ntfy_vma->addr;
636 args.limit += chan->ntfy_vma->addr;
638 if (drm->agp.bridge) {
639 args.target = NV_DMA_V0_TARGET_AGP;
640 args.access = NV_DMA_V0_ACCESS_RDWR;
641 args.start += drm->agp.base + chan->ntfy->offset;
642 args.limit += drm->agp.base + chan->ntfy->offset;
644 args.target = NV_DMA_V0_TARGET_VM;
645 args.access = NV_DMA_V0_ACCESS_RDWR;
646 args.start += chan->ntfy->offset;
647 args.limit += chan->ntfy->offset;
650 client->route = NVDRM_OBJECT_ABI16;
651 ret = nvif_object_ctor(&chan->chan->user, "abi16Ntfy", info->handle,
652 NV_DMA_IN_MEMORY, &args, sizeof(args),
654 client->route = NVDRM_OBJECT_NVIF;
658 info->offset = ntfy->node->offset;
661 nouveau_abi16_ntfy_fini(chan, ntfy);
662 return nouveau_abi16_put(abi16, ret);
666 nouveau_abi16_ioctl_gpuobj_free(ABI16_IOCTL_ARGS)
668 struct drm_nouveau_gpuobj_free *fini = data;
669 struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv);
670 struct nouveau_abi16_chan *chan;
671 struct nouveau_abi16_ntfy *ntfy;
674 if (unlikely(!abi16))
677 chan = nouveau_abi16_chan(abi16, fini->channel);
679 return nouveau_abi16_put(abi16, -EINVAL);
681 /* synchronize with the user channel and destroy the gpu object */
682 nouveau_channel_idle(chan->chan);
684 list_for_each_entry(ntfy, &chan->notifiers, head) {
685 if (ntfy->object.handle == fini->handle) {
686 nouveau_abi16_ntfy_fini(chan, ntfy);
692 return nouveau_abi16_put(abi16, ret);