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