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>
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"
35 struct nouveau_abi16 *
36 nouveau_abi16_get(struct drm_file *file_priv, struct drm_device *dev)
38 struct nouveau_cli *cli = nouveau_cli(file_priv);
39 mutex_lock(&cli->mutex);
41 struct nouveau_abi16 *abi16;
42 cli->abi16 = abi16 = kzalloc(sizeof(*abi16), GFP_KERNEL);
44 struct nv_device_v0 args = {
48 INIT_LIST_HEAD(&abi16->channels);
50 /* allocate device object targeting client's default
51 * device (ie. the one that belongs to the fd it
54 if (nvif_device_init(&cli->base.object,
55 NOUVEAU_ABI16_DEVICE, NV_DEVICE,
64 mutex_unlock(&cli->mutex);
70 nouveau_abi16_put(struct nouveau_abi16 *abi16, int ret)
72 struct nouveau_cli *cli = (void *)abi16->device.object.client;
73 mutex_unlock(&cli->mutex);
78 nouveau_abi16_swclass(struct nouveau_drm *drm)
80 switch (drm->device.info.family) {
81 case NV_DEVICE_INFO_V0_TNT:
82 return NVIF_IOCTL_NEW_V0_SW_NV04;
83 case NV_DEVICE_INFO_V0_CELSIUS:
84 case NV_DEVICE_INFO_V0_KELVIN:
85 case NV_DEVICE_INFO_V0_RANKINE:
86 case NV_DEVICE_INFO_V0_CURIE:
87 return NVIF_IOCTL_NEW_V0_SW_NV10;
88 case NV_DEVICE_INFO_V0_TESLA:
89 return NVIF_IOCTL_NEW_V0_SW_NV50;
90 case NV_DEVICE_INFO_V0_FERMI:
91 case NV_DEVICE_INFO_V0_KEPLER:
92 case NV_DEVICE_INFO_V0_MAXWELL:
93 return NVIF_IOCTL_NEW_V0_SW_GF100;
100 nouveau_abi16_ntfy_fini(struct nouveau_abi16_chan *chan,
101 struct nouveau_abi16_ntfy *ntfy)
103 nvif_object_fini(&ntfy->object);
104 nvkm_mm_free(&chan->heap, &ntfy->node);
105 list_del(&ntfy->head);
110 nouveau_abi16_chan_fini(struct nouveau_abi16 *abi16,
111 struct nouveau_abi16_chan *chan)
113 struct nouveau_abi16_ntfy *ntfy, *temp;
115 /* wait for all activity to stop before releasing notify object, which
116 * may be still in use */
117 if (chan->chan && chan->ntfy)
118 nouveau_channel_idle(chan->chan);
120 /* cleanup notifier state */
121 list_for_each_entry_safe(ntfy, temp, &chan->notifiers, head) {
122 nouveau_abi16_ntfy_fini(chan, ntfy);
126 nouveau_bo_vma_del(chan->ntfy, &chan->ntfy_vma);
127 nouveau_bo_unpin(chan->ntfy);
128 drm_gem_object_unreference_unlocked(&chan->ntfy->gem);
131 if (chan->heap.block_size)
132 nvkm_mm_fini(&chan->heap);
134 /* destroy channel object, all children will be killed too */
136 abi16->handles &= ~(1ULL << (chan->chan->user.handle & 0xffff));
137 nouveau_channel_del(&chan->chan);
140 list_del(&chan->head);
145 nouveau_abi16_fini(struct nouveau_abi16 *abi16)
147 struct nouveau_cli *cli = (void *)abi16->device.object.client;
148 struct nouveau_abi16_chan *chan, *temp;
150 /* cleanup channels */
151 list_for_each_entry_safe(chan, temp, &abi16->channels, head) {
152 nouveau_abi16_chan_fini(abi16, chan);
155 /* destroy the device object */
156 nvif_device_fini(&abi16->device);
163 nouveau_abi16_ioctl_getparam(ABI16_IOCTL_ARGS)
165 struct nouveau_cli *cli = nouveau_cli(file_priv);
166 struct nouveau_drm *drm = nouveau_drm(dev);
167 struct nvif_device *device = &drm->device;
168 struct nvkm_gr *gr = nvxx_gr(device);
169 struct drm_nouveau_getparam *getparam = data;
171 switch (getparam->param) {
172 case NOUVEAU_GETPARAM_CHIPSET_ID:
173 getparam->value = device->info.chipset;
175 case NOUVEAU_GETPARAM_PCI_VENDOR:
176 if (nv_device_is_pci(nvxx_device(device)))
177 getparam->value = dev->pdev->vendor;
181 case NOUVEAU_GETPARAM_PCI_DEVICE:
182 if (nv_device_is_pci(nvxx_device(device)))
183 getparam->value = dev->pdev->device;
187 case NOUVEAU_GETPARAM_BUS_TYPE:
188 if (!nv_device_is_pci(nvxx_device(device)))
191 if (drm_pci_device_is_agp(dev))
194 if (!pci_is_pcie(dev->pdev))
199 case NOUVEAU_GETPARAM_FB_SIZE:
200 getparam->value = drm->gem.vram_available;
202 case NOUVEAU_GETPARAM_AGP_SIZE:
203 getparam->value = drm->gem.gart_available;
205 case NOUVEAU_GETPARAM_VM_VRAM_BASE:
206 getparam->value = 0; /* deprecated */
208 case NOUVEAU_GETPARAM_PTIMER_TIME:
209 getparam->value = nvif_device_time(device);
211 case NOUVEAU_GETPARAM_HAS_BO_USAGE:
214 case NOUVEAU_GETPARAM_HAS_PAGEFLIP:
217 case NOUVEAU_GETPARAM_GRAPH_UNITS:
218 getparam->value = gr->units ? gr->units(gr) : 0;
221 NV_PRINTK(dbg, cli, "unknown parameter %lld\n", getparam->param);
229 nouveau_abi16_ioctl_setparam(ABI16_IOCTL_ARGS)
235 nouveau_abi16_ioctl_channel_alloc(ABI16_IOCTL_ARGS)
237 struct drm_nouveau_channel_alloc *init = data;
238 struct nouveau_cli *cli = nouveau_cli(file_priv);
239 struct nouveau_drm *drm = nouveau_drm(dev);
240 struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
241 struct nouveau_abi16_chan *chan;
242 struct nvif_device *device;
245 if (unlikely(!abi16))
249 return nouveau_abi16_put(abi16, -ENODEV);
251 device = &abi16->device;
253 /* hack to allow channel engine type specification on kepler */
254 if (device->info.family >= NV_DEVICE_INFO_V0_KEPLER) {
255 if (init->fb_ctxdma_handle != ~0)
256 init->fb_ctxdma_handle = KEPLER_CHANNEL_GPFIFO_A_V0_ENGINE_GR;
258 init->fb_ctxdma_handle = init->tt_ctxdma_handle;
260 /* allow flips to be executed if this is a graphics channel */
261 init->tt_ctxdma_handle = 0;
262 if (init->fb_ctxdma_handle == KEPLER_CHANNEL_GPFIFO_A_V0_ENGINE_GR)
263 init->tt_ctxdma_handle = 1;
266 if (init->fb_ctxdma_handle == ~0 || init->tt_ctxdma_handle == ~0)
267 return nouveau_abi16_put(abi16, -EINVAL);
269 /* allocate "abi16 channel" data and make up a handle for it */
270 init->channel = __ffs64(~abi16->handles);
271 if (~abi16->handles == 0)
272 return nouveau_abi16_put(abi16, -ENOSPC);
274 chan = kzalloc(sizeof(*chan), GFP_KERNEL);
276 return nouveau_abi16_put(abi16, -ENOMEM);
278 INIT_LIST_HEAD(&chan->notifiers);
279 list_add(&chan->head, &abi16->channels);
280 abi16->handles |= (1ULL << init->channel);
282 /* create channel object and initialise dma and fence management */
283 ret = nouveau_channel_new(drm, device,
284 NOUVEAU_ABI16_CHAN(init->channel),
285 init->fb_ctxdma_handle,
286 init->tt_ctxdma_handle, &chan->chan);
290 if (device->info.family >= NV_DEVICE_INFO_V0_TESLA)
291 init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_VRAM |
292 NOUVEAU_GEM_DOMAIN_GART;
294 if (chan->chan->push.buffer->bo.mem.mem_type == TTM_PL_VRAM)
295 init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_VRAM;
297 init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_GART;
299 if (device->info.family < NV_DEVICE_INFO_V0_CELSIUS) {
300 init->subchan[0].handle = 0x00000000;
301 init->subchan[0].grclass = 0x0000;
302 init->subchan[1].handle = chan->chan->nvsw.handle;
303 init->subchan[1].grclass = 0x506e;
304 init->nr_subchan = 2;
307 /* Named memory object area */
308 ret = nouveau_gem_new(dev, PAGE_SIZE, 0, NOUVEAU_GEM_DOMAIN_GART,
311 ret = nouveau_bo_pin(chan->ntfy, TTM_PL_FLAG_TT, false);
315 if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
316 ret = nouveau_bo_vma_add(chan->ntfy, cli->vm,
322 ret = drm_gem_handle_create(file_priv, &chan->ntfy->gem,
323 &init->notifier_handle);
327 ret = nvkm_mm_init(&chan->heap, 0, PAGE_SIZE, 1);
330 nouveau_abi16_chan_fini(abi16, chan);
331 return nouveau_abi16_put(abi16, ret);
334 static struct nouveau_abi16_chan *
335 nouveau_abi16_chan(struct nouveau_abi16 *abi16, int channel)
337 struct nouveau_abi16_chan *chan;
339 list_for_each_entry(chan, &abi16->channels, head) {
340 if (chan->chan->user.handle == NOUVEAU_ABI16_CHAN(channel))
348 nouveau_abi16_ioctl_channel_free(ABI16_IOCTL_ARGS)
350 struct drm_nouveau_channel_free *req = data;
351 struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
352 struct nouveau_abi16_chan *chan;
354 if (unlikely(!abi16))
357 chan = nouveau_abi16_chan(abi16, req->channel);
359 return nouveau_abi16_put(abi16, -ENOENT);
360 nouveau_abi16_chan_fini(abi16, chan);
361 return nouveau_abi16_put(abi16, 0);
365 nouveau_abi16_ioctl_grobj_alloc(ABI16_IOCTL_ARGS)
367 struct drm_nouveau_grobj_alloc *init = data;
368 struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
369 struct nouveau_abi16_chan *chan;
370 struct nouveau_abi16_ntfy *ntfy;
371 struct nvif_client *client;
376 if (unlikely(!abi16))
379 if (init->handle == ~0)
380 return nouveau_abi16_put(abi16, -EINVAL);
381 client = abi16->device.object.client;
383 chan = nouveau_abi16_chan(abi16, init->channel);
385 return nouveau_abi16_put(abi16, -ENOENT);
387 ret = nvif_object_sclass(&chan->chan->user, sclass, ARRAY_SIZE(sclass));
389 return nouveau_abi16_put(abi16, ret);
391 if ((init->class & 0x00ff) == 0x006e) {
392 /* nvsw: compatibility with older 0x*6e class identifier */
393 for (i = 0; !oclass && i < ret; i++) {
395 case NVIF_IOCTL_NEW_V0_SW_NV04:
396 case NVIF_IOCTL_NEW_V0_SW_NV10:
397 case NVIF_IOCTL_NEW_V0_SW_NV50:
398 case NVIF_IOCTL_NEW_V0_SW_GF100:
406 if ((init->class & 0x00ff) == 0x00b1) {
407 /* msvld: compatibility with incorrect version exposure */
408 for (i = 0; i < ret; i++) {
409 if ((sclass[i] & 0x00ff) == 0x00b1) {
415 if ((init->class & 0x00ff) == 0x00b2) { /* mspdec */
416 /* mspdec: compatibility with incorrect version exposure */
417 for (i = 0; i < ret; i++) {
418 if ((sclass[i] & 0x00ff) == 0x00b2) {
424 if ((init->class & 0x00ff) == 0x00b3) { /* msppp */
425 /* msppp: compatibility with incorrect version exposure */
426 for (i = 0; i < ret; i++) {
427 if ((sclass[i] & 0x00ff) == 0x00b3) {
433 oclass = init->class;
437 return nouveau_abi16_put(abi16, -EINVAL);
439 ntfy = kzalloc(sizeof(*ntfy), GFP_KERNEL);
441 return nouveau_abi16_put(abi16, -ENOMEM);
443 list_add(&ntfy->head, &chan->notifiers);
445 client->route = NVDRM_OBJECT_ABI16;
446 ret = nvif_object_init(&chan->chan->user, init->handle, oclass,
447 NULL, 0, &ntfy->object);
448 client->route = NVDRM_OBJECT_NVIF;
451 nouveau_abi16_ntfy_fini(chan, ntfy);
452 return nouveau_abi16_put(abi16, ret);
456 nouveau_abi16_ioctl_notifierobj_alloc(ABI16_IOCTL_ARGS)
458 struct drm_nouveau_notifierobj_alloc *info = data;
459 struct nouveau_drm *drm = nouveau_drm(dev);
460 struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
461 struct nouveau_abi16_chan *chan;
462 struct nouveau_abi16_ntfy *ntfy;
463 struct nvif_device *device = &abi16->device;
464 struct nvif_client *client;
465 struct nv_dma_v0 args = {};
468 if (unlikely(!abi16))
471 /* completely unnecessary for these chipsets... */
472 if (unlikely(device->info.family >= NV_DEVICE_INFO_V0_FERMI))
473 return nouveau_abi16_put(abi16, -EINVAL);
474 client = abi16->device.object.client;
476 chan = nouveau_abi16_chan(abi16, info->channel);
478 return nouveau_abi16_put(abi16, -ENOENT);
480 ntfy = kzalloc(sizeof(*ntfy), GFP_KERNEL);
482 return nouveau_abi16_put(abi16, -ENOMEM);
484 list_add(&ntfy->head, &chan->notifiers);
486 ret = nvkm_mm_head(&chan->heap, 0, 1, info->size, info->size, 1,
491 args.start = ntfy->node->offset;
492 args.limit = ntfy->node->offset + ntfy->node->length - 1;
493 if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
494 args.target = NV_DMA_V0_TARGET_VM;
495 args.access = NV_DMA_V0_ACCESS_VM;
496 args.start += chan->ntfy_vma.offset;
497 args.limit += chan->ntfy_vma.offset;
499 if (drm->agp.stat == ENABLED) {
500 args.target = NV_DMA_V0_TARGET_AGP;
501 args.access = NV_DMA_V0_ACCESS_RDWR;
502 args.start += drm->agp.base + chan->ntfy->bo.offset;
503 args.limit += drm->agp.base + chan->ntfy->bo.offset;
505 args.target = NV_DMA_V0_TARGET_VM;
506 args.access = NV_DMA_V0_ACCESS_RDWR;
507 args.start += chan->ntfy->bo.offset;
508 args.limit += chan->ntfy->bo.offset;
511 client->route = NVDRM_OBJECT_ABI16;
512 client->super = true;
513 ret = nvif_object_init(&chan->chan->user, info->handle,
514 NV_DMA_IN_MEMORY, &args, sizeof(args),
516 client->super = false;
517 client->route = NVDRM_OBJECT_NVIF;
521 info->offset = ntfy->node->offset;
524 nouveau_abi16_ntfy_fini(chan, ntfy);
525 return nouveau_abi16_put(abi16, ret);
529 nouveau_abi16_ioctl_gpuobj_free(ABI16_IOCTL_ARGS)
531 struct drm_nouveau_gpuobj_free *fini = data;
532 struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
533 struct nouveau_abi16_chan *chan;
534 struct nouveau_abi16_ntfy *ntfy;
537 if (unlikely(!abi16))
540 chan = nouveau_abi16_chan(abi16, fini->channel);
542 return nouveau_abi16_put(abi16, -EINVAL);
544 /* synchronize with the user channel and destroy the gpu object */
545 nouveau_channel_idle(chan->chan);
547 list_for_each_entry(ntfy, &chan->notifiers, head) {
548 if (ntfy->object.handle == fini->handle) {
549 nouveau_abi16_ntfy_fini(chan, ntfy);
555 return nouveau_abi16_put(abi16, ret);