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