drm/nouveau/nvif: simplify and tidy library interfaces
[linux-2.6-block.git] / drivers / gpu / drm / nouveau / nouveau_abi16.c
CommitLineData
2a259a3d
BS
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
a4e610b5
BS
24#include <nvif/client.h>
25#include <nvif/driver.h>
26#include <nvif/ioctl.h>
fdb751ef 27#include <nvif/class.h>
2a259a3d 28
ebb945a9 29#include "nouveau_drm.h"
2a259a3d 30#include "nouveau_dma.h"
ebb945a9
BS
31#include "nouveau_gem.h"
32#include "nouveau_chan.h"
2a259a3d 33#include "nouveau_abi16.h"
ebb945a9
BS
34
35struct nouveau_abi16 *
36nouveau_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) {
586491e6
BS
44 struct nv_device_v0 args = {
45 .device = ~0ULL,
46 };
47
ebb945a9 48 INIT_LIST_HEAD(&abi16->channels);
ebb945a9
BS
49
50 /* allocate device object targeting client's default
51 * device (ie. the one that belongs to the fd it
52 * opened)
53 */
a01ca78c 54 if (nvif_device_init(&cli->base.object,
a4e610b5 55 NOUVEAU_ABI16_DEVICE, NV_DEVICE,
586491e6 56 &args, sizeof(args),
0ad72863 57 &abi16->device) == 0)
ebb945a9
BS
58 return cli->abi16;
59
60 kfree(cli->abi16);
61 cli->abi16 = NULL;
62 }
63
64 mutex_unlock(&cli->mutex);
65 }
66 return cli->abi16;
67}
68
69int
70nouveau_abi16_put(struct nouveau_abi16 *abi16, int ret)
71{
a01ca78c 72 struct nouveau_cli *cli = (void *)abi16->device.object.client;
ebb945a9
BS
73 mutex_unlock(&cli->mutex);
74 return ret;
75}
76
77u16
78nouveau_abi16_swclass(struct nouveau_drm *drm)
79{
967e7bde
BS
80 switch (drm->device.info.family) {
81 case NV_DEVICE_INFO_V0_TNT:
ebb945a9 82 return 0x006e;
967e7bde
BS
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:
ebb945a9 87 return 0x016e;
967e7bde 88 case NV_DEVICE_INFO_V0_TESLA:
ebb945a9 89 return 0x506e;
967e7bde
BS
90 case NV_DEVICE_INFO_V0_FERMI:
91 case NV_DEVICE_INFO_V0_KEPLER:
92 case NV_DEVICE_INFO_V0_MAXWELL:
ebb945a9
BS
93 return 0x906e;
94 }
95
96 return 0x0000;
97}
98
99static void
100nouveau_abi16_ntfy_fini(struct nouveau_abi16_chan *chan,
101 struct nouveau_abi16_ntfy *ntfy)
102{
a01ca78c 103 nvif_object_fini(&ntfy->object);
be83cd4e 104 nvkm_mm_free(&chan->heap, &ntfy->node);
ebb945a9
BS
105 list_del(&ntfy->head);
106 kfree(ntfy);
107}
108
109static void
110nouveau_abi16_chan_fini(struct nouveau_abi16 *abi16,
111 struct nouveau_abi16_chan *chan)
112{
113 struct nouveau_abi16_ntfy *ntfy, *temp;
114
2b77c1c0
MS
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);
119
ebb945a9
BS
120 /* cleanup notifier state */
121 list_for_each_entry_safe(ntfy, temp, &chan->notifiers, head) {
122 nouveau_abi16_ntfy_fini(chan, ntfy);
123 }
124
125 if (chan->ntfy) {
126 nouveau_bo_vma_del(chan->ntfy, &chan->ntfy_vma);
198c14a0 127 nouveau_bo_unpin(chan->ntfy);
55fb74ad 128 drm_gem_object_unreference_unlocked(&chan->ntfy->gem);
ebb945a9
BS
129 }
130
131 if (chan->heap.block_size)
be83cd4e 132 nvkm_mm_fini(&chan->heap);
ebb945a9
BS
133
134 /* destroy channel object, all children will be killed too */
135 if (chan->chan) {
a01ca78c 136 abi16->handles &= ~(1ULL << (chan->chan->user.handle & 0xffff));
ebb945a9
BS
137 nouveau_channel_del(&chan->chan);
138 }
139
140 list_del(&chan->head);
141 kfree(chan);
142}
143
144void
145nouveau_abi16_fini(struct nouveau_abi16 *abi16)
146{
a01ca78c 147 struct nouveau_cli *cli = (void *)abi16->device.object.client;
ebb945a9
BS
148 struct nouveau_abi16_chan *chan, *temp;
149
150 /* cleanup channels */
151 list_for_each_entry_safe(chan, temp, &abi16->channels, head) {
152 nouveau_abi16_chan_fini(abi16, chan);
153 }
154
155 /* destroy the device object */
0ad72863 156 nvif_device_fini(&abi16->device);
ebb945a9
BS
157
158 kfree(cli->abi16);
159 cli->abi16 = NULL;
160}
2a259a3d
BS
161
162int
163nouveau_abi16_ioctl_getparam(ABI16_IOCTL_ARGS)
164{
fa2bade9 165 struct nouveau_cli *cli = nouveau_cli(file_priv);
ebb945a9 166 struct nouveau_drm *drm = nouveau_drm(dev);
967e7bde 167 struct nvif_device *device = &drm->device;
be83cd4e 168 struct nvkm_gr *gr = nvxx_gr(device);
2a259a3d
BS
169 struct drm_nouveau_getparam *getparam = data;
170
171 switch (getparam->param) {
172 case NOUVEAU_GETPARAM_CHIPSET_ID:
967e7bde 173 getparam->value = device->info.chipset;
2a259a3d
BS
174 break;
175 case NOUVEAU_GETPARAM_PCI_VENDOR:
989aa5b7 176 if (nv_device_is_pci(nvxx_device(device)))
420b9469
AC
177 getparam->value = dev->pdev->vendor;
178 else
179 getparam->value = 0;
2a259a3d
BS
180 break;
181 case NOUVEAU_GETPARAM_PCI_DEVICE:
989aa5b7 182 if (nv_device_is_pci(nvxx_device(device)))
420b9469
AC
183 getparam->value = dev->pdev->device;
184 else
185 getparam->value = 0;
2a259a3d
BS
186 break;
187 case NOUVEAU_GETPARAM_BUS_TYPE:
989aa5b7 188 if (!nv_device_is_pci(nvxx_device(device)))
420b9469
AC
189 getparam->value = 3;
190 else
2a259a3d
BS
191 if (drm_pci_device_is_agp(dev))
192 getparam->value = 0;
193 else
194 if (!pci_is_pcie(dev->pdev))
195 getparam->value = 1;
196 else
197 getparam->value = 2;
198 break;
199 case NOUVEAU_GETPARAM_FB_SIZE:
ebb945a9 200 getparam->value = drm->gem.vram_available;
2a259a3d
BS
201 break;
202 case NOUVEAU_GETPARAM_AGP_SIZE:
ebb945a9 203 getparam->value = drm->gem.gart_available;
2a259a3d
BS
204 break;
205 case NOUVEAU_GETPARAM_VM_VRAM_BASE:
206 getparam->value = 0; /* deprecated */
207 break;
208 case NOUVEAU_GETPARAM_PTIMER_TIME:
56f67dc1 209 getparam->value = nvif_device_time(device);
2a259a3d
BS
210 break;
211 case NOUVEAU_GETPARAM_HAS_BO_USAGE:
212 getparam->value = 1;
213 break;
214 case NOUVEAU_GETPARAM_HAS_PAGEFLIP:
215 getparam->value = 1;
216 break;
217 case NOUVEAU_GETPARAM_GRAPH_UNITS:
b8bf04e1 218 getparam->value = gr->units ? gr->units(gr) : 0;
7e22e71e 219 break;
2a259a3d 220 default:
9ad97ede 221 NV_PRINTK(dbg, cli, "unknown parameter %lld\n", getparam->param);
2a259a3d
BS
222 return -EINVAL;
223 }
224
225 return 0;
226}
227
228int
229nouveau_abi16_ioctl_setparam(ABI16_IOCTL_ARGS)
230{
231 return -EINVAL;
232}
233
234int
235nouveau_abi16_ioctl_channel_alloc(ABI16_IOCTL_ARGS)
236{
2a259a3d 237 struct drm_nouveau_channel_alloc *init = data;
ebb945a9
BS
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;
967e7bde 242 struct nvif_device *device;
2a259a3d
BS
243 int ret;
244
ebb945a9
BS
245 if (unlikely(!abi16))
246 return -ENOMEM;
bf7e438b
MS
247
248 if (!drm->channel)
249 return nouveau_abi16_put(abi16, -ENODEV);
250
967e7bde 251 device = &abi16->device;
ebb945a9 252
49469800 253 /* hack to allow channel engine type specification on kepler */
967e7bde 254 if (device->info.family >= NV_DEVICE_INFO_V0_KEPLER) {
49469800 255 if (init->fb_ctxdma_handle != ~0)
bbf8906b 256 init->fb_ctxdma_handle = KEPLER_CHANNEL_GPFIFO_A_V0_ENGINE_GR;
49469800
BS
257 else
258 init->fb_ctxdma_handle = init->tt_ctxdma_handle;
259
260 /* allow flips to be executed if this is a graphics channel */
261 init->tt_ctxdma_handle = 0;
bbf8906b 262 if (init->fb_ctxdma_handle == KEPLER_CHANNEL_GPFIFO_A_V0_ENGINE_GR)
49469800
BS
263 init->tt_ctxdma_handle = 1;
264 }
265
266 if (init->fb_ctxdma_handle == ~0 || init->tt_ctxdma_handle == ~0)
267 return nouveau_abi16_put(abi16, -EINVAL);
268
ebb945a9 269 /* allocate "abi16 channel" data and make up a handle for it */
73970c47
IM
270 init->channel = __ffs64(~abi16->handles);
271 if (~abi16->handles == 0)
ebb945a9
BS
272 return nouveau_abi16_put(abi16, -ENOSPC);
273
274 chan = kzalloc(sizeof(*chan), GFP_KERNEL);
275 if (!chan)
276 return nouveau_abi16_put(abi16, -ENOMEM);
277
278 INIT_LIST_HEAD(&chan->notifiers);
279 list_add(&chan->head, &abi16->channels);
ef98c1f7 280 abi16->handles |= (1ULL << init->channel);
2a259a3d 281
ebb945a9 282 /* create channel object and initialise dma and fence management */
a4e610b5
BS
283 ret = nouveau_channel_new(drm, device,
284 NOUVEAU_ABI16_CHAN(init->channel),
0ad72863 285 init->fb_ctxdma_handle,
ebb945a9 286 init->tt_ctxdma_handle, &chan->chan);
2a259a3d 287 if (ret)
ebb945a9
BS
288 goto done;
289
967e7bde 290 if (device->info.family >= NV_DEVICE_INFO_V0_TESLA)
ebb945a9
BS
291 init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_VRAM |
292 NOUVEAU_GEM_DOMAIN_GART;
293 else
294 if (chan->chan->push.buffer->bo.mem.mem_type == TTM_PL_VRAM)
2a259a3d 295 init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_VRAM;
ebb945a9
BS
296 else
297 init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_GART;
2a259a3d 298
967e7bde 299 if (device->info.family < NV_DEVICE_INFO_V0_CELSIUS) {
2a259a3d
BS
300 init->subchan[0].handle = 0x00000000;
301 init->subchan[0].grclass = 0x0000;
f45f55c4 302 init->subchan[1].handle = chan->chan->nvsw.handle;
ebb945a9 303 init->subchan[1].grclass = 0x506e;
2a259a3d
BS
304 init->nr_subchan = 2;
305 }
306
307 /* Named memory object area */
ebb945a9
BS
308 ret = nouveau_gem_new(dev, PAGE_SIZE, 0, NOUVEAU_GEM_DOMAIN_GART,
309 0, 0, &chan->ntfy);
310 if (ret == 0)
ad76b3f7 311 ret = nouveau_bo_pin(chan->ntfy, TTM_PL_FLAG_TT, false);
ebb945a9
BS
312 if (ret)
313 goto done;
314
967e7bde 315 if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
0ad72863 316 ret = nouveau_bo_vma_add(chan->ntfy, cli->vm,
ebb945a9
BS
317 &chan->ntfy_vma);
318 if (ret)
319 goto done;
320 }
321
55fb74ad 322 ret = drm_gem_handle_create(file_priv, &chan->ntfy->gem,
2a259a3d 323 &init->notifier_handle);
ebb945a9
BS
324 if (ret)
325 goto done;
2a259a3d 326
be83cd4e 327 ret = nvkm_mm_init(&chan->heap, 0, PAGE_SIZE, 1);
ebb945a9
BS
328done:
329 if (ret)
330 nouveau_abi16_chan_fini(abi16, chan);
331 return nouveau_abi16_put(abi16, ret);
2a259a3d
BS
332}
333
a4e610b5
BS
334static struct nouveau_abi16_chan *
335nouveau_abi16_chan(struct nouveau_abi16 *abi16, int channel)
336{
337 struct nouveau_abi16_chan *chan;
338
339 list_for_each_entry(chan, &abi16->channels, head) {
a01ca78c 340 if (chan->chan->user.handle == NOUVEAU_ABI16_CHAN(channel))
a4e610b5
BS
341 return chan;
342 }
343
344 return NULL;
345}
ebb945a9 346
2a259a3d
BS
347int
348nouveau_abi16_ioctl_channel_free(ABI16_IOCTL_ARGS)
349{
350 struct drm_nouveau_channel_free *req = data;
ebb945a9
BS
351 struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
352 struct nouveau_abi16_chan *chan;
2a259a3d 353
ebb945a9
BS
354 if (unlikely(!abi16))
355 return -ENOMEM;
2a259a3d 356
a4e610b5
BS
357 chan = nouveau_abi16_chan(abi16, req->channel);
358 if (!chan)
359 return nouveau_abi16_put(abi16, -ENOENT);
360 nouveau_abi16_chan_fini(abi16, chan);
361 return nouveau_abi16_put(abi16, 0);
2a259a3d
BS
362}
363
364int
365nouveau_abi16_ioctl_grobj_alloc(ABI16_IOCTL_ARGS)
366{
367 struct drm_nouveau_grobj_alloc *init = data;
ebb945a9 368 struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
a01ca78c
BS
369 struct nouveau_abi16_chan *chan;
370 struct nouveau_abi16_ntfy *ntfy;
ebb945a9 371 struct nouveau_drm *drm = nouveau_drm(dev);
a4e610b5 372 struct nvif_client *client;
2a259a3d
BS
373 int ret;
374
ebb945a9
BS
375 if (unlikely(!abi16))
376 return -ENOMEM;
377
2a259a3d 378 if (init->handle == ~0)
ebb945a9 379 return nouveau_abi16_put(abi16, -EINVAL);
a01ca78c 380 client = abi16->device.object.client;
2a259a3d
BS
381
382 /* compatibility with userspace that assumes 506e for all chipsets */
383 if (init->class == 0x506e) {
ebb945a9 384 init->class = nouveau_abi16_swclass(drm);
2a259a3d 385 if (init->class == 0x906e)
ebb945a9 386 return nouveau_abi16_put(abi16, 0);
2a259a3d
BS
387 }
388
a01ca78c
BS
389 chan = nouveau_abi16_chan(abi16, init->channel);
390 if (!chan)
391 return nouveau_abi16_put(abi16, -ENOENT);
392
393 ntfy = kzalloc(sizeof(*ntfy), GFP_KERNEL);
394 if (!ntfy)
395 return nouveau_abi16_put(abi16, -ENOMEM);
396
397 list_add(&ntfy->head, &chan->notifiers);
398
399 client->route = NVDRM_OBJECT_ABI16;
400 ret = nvif_object_init(&chan->chan->user, init->handle, init->class,
401 NULL, 0, &ntfy->object);
402 client->route = NVDRM_OBJECT_NVIF;
403
404 if (ret)
405 nouveau_abi16_ntfy_fini(chan, ntfy);
ebb945a9 406 return nouveau_abi16_put(abi16, ret);
2a259a3d
BS
407}
408
409int
410nouveau_abi16_ioctl_notifierobj_alloc(ABI16_IOCTL_ARGS)
411{
ebb945a9
BS
412 struct drm_nouveau_notifierobj_alloc *info = data;
413 struct nouveau_drm *drm = nouveau_drm(dev);
ebb945a9 414 struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
a4e610b5 415 struct nouveau_abi16_chan *chan;
ebb945a9 416 struct nouveau_abi16_ntfy *ntfy;
967e7bde 417 struct nvif_device *device = &abi16->device;
3bdda04f 418 struct nvif_client *client;
a01ca78c 419 struct nv_dma_v0 args = {};
2a259a3d
BS
420 int ret;
421
ebb945a9
BS
422 if (unlikely(!abi16))
423 return -ENOMEM;
424
2a259a3d 425 /* completely unnecessary for these chipsets... */
967e7bde 426 if (unlikely(device->info.family >= NV_DEVICE_INFO_V0_FERMI))
ebb945a9 427 return nouveau_abi16_put(abi16, -EINVAL);
a01ca78c 428 client = abi16->device.object.client;
2a259a3d 429
a4e610b5 430 chan = nouveau_abi16_chan(abi16, info->channel);
ebb945a9
BS
431 if (!chan)
432 return nouveau_abi16_put(abi16, -ENOENT);
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);
ebb945a9 439
be83cd4e
BS
440 ret = nvkm_mm_head(&chan->heap, 0, 1, info->size, info->size, 1,
441 &ntfy->node);
ebb945a9
BS
442 if (ret)
443 goto done;
444
a01ca78c
BS
445 args.start = ntfy->node->offset;
446 args.limit = ntfy->node->offset + ntfy->node->length - 1;
967e7bde 447 if (device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
a01ca78c
BS
448 args.target = NV_DMA_V0_TARGET_VM;
449 args.access = NV_DMA_V0_ACCESS_VM;
450 args.start += chan->ntfy_vma.offset;
451 args.limit += chan->ntfy_vma.offset;
ebb945a9
BS
452 } else
453 if (drm->agp.stat == ENABLED) {
a01ca78c
BS
454 args.target = NV_DMA_V0_TARGET_AGP;
455 args.access = NV_DMA_V0_ACCESS_RDWR;
456 args.start += drm->agp.base + chan->ntfy->bo.offset;
457 args.limit += drm->agp.base + chan->ntfy->bo.offset;
ebb945a9 458 } else {
a01ca78c
BS
459 args.target = NV_DMA_V0_TARGET_VM;
460 args.access = NV_DMA_V0_ACCESS_RDWR;
461 args.start += chan->ntfy->bo.offset;
462 args.limit += chan->ntfy->bo.offset;
ebb945a9
BS
463 }
464
a01ca78c
BS
465 client->route = NVDRM_OBJECT_ABI16;
466 client->super = true;
467 ret = nvif_object_init(&chan->chan->user, info->handle,
468 NV_DMA_IN_MEMORY, &args, sizeof(args),
469 &ntfy->object);
3bdda04f 470 client->super = false;
a01ca78c 471 client->route = NVDRM_OBJECT_NVIF;
ebb945a9
BS
472 if (ret)
473 goto done;
474
c1ccaa64 475 info->offset = ntfy->node->offset;
ebb945a9
BS
476done:
477 if (ret)
478 nouveau_abi16_ntfy_fini(chan, ntfy);
479 return nouveau_abi16_put(abi16, ret);
2a259a3d
BS
480}
481
482int
483nouveau_abi16_ioctl_gpuobj_free(ABI16_IOCTL_ARGS)
484{
ebb945a9
BS
485 struct drm_nouveau_gpuobj_free *fini = data;
486 struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
a4e610b5 487 struct nouveau_abi16_chan *chan;
ebb945a9 488 struct nouveau_abi16_ntfy *ntfy;
a01ca78c 489 int ret = -ENOENT;
2a259a3d 490
ebb945a9
BS
491 if (unlikely(!abi16))
492 return -ENOMEM;
493
a4e610b5 494 chan = nouveau_abi16_chan(abi16, fini->channel);
ebb945a9 495 if (!chan)
a01ca78c 496 return nouveau_abi16_put(abi16, -EINVAL);
2a259a3d 497
ebb945a9
BS
498 /* synchronize with the user channel and destroy the gpu object */
499 nouveau_channel_idle(chan->chan);
2a259a3d 500
ebb945a9 501 list_for_each_entry(ntfy, &chan->notifiers, head) {
a01ca78c
BS
502 if (ntfy->object.handle == fini->handle) {
503 nouveau_abi16_ntfy_fini(chan, ntfy);
504 ret = 0;
ebb945a9
BS
505 break;
506 }
507 }
508
a01ca78c 509 return nouveau_abi16_put(abi16, ret);
2a259a3d 510}