drm/nouveau/fence: minor api changes for an upcoming rework
[pandora-kernel.git] / drivers / gpu / drm / nouveau / nouveau_channel.c
1 /*
2  * Copyright 2005-2006 Stephane Marchesin
3  * All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24
25 #include "drmP.h"
26 #include "drm.h"
27 #include "nouveau_drv.h"
28 #include "nouveau_drm.h"
29 #include "nouveau_dma.h"
30 #include "nouveau_ramht.h"
31 #include "nouveau_fence.h"
32 #include "nouveau_software.h"
33
34 static int
35 nouveau_channel_pushbuf_init(struct nouveau_channel *chan)
36 {
37         u32 mem = nouveau_vram_pushbuf ? TTM_PL_FLAG_VRAM : TTM_PL_FLAG_TT;
38         struct drm_device *dev = chan->dev;
39         struct drm_nouveau_private *dev_priv = dev->dev_private;
40         int ret;
41
42         /* allocate buffer object */
43         ret = nouveau_bo_new(dev, 65536, 0, mem, 0, 0, NULL, &chan->pushbuf_bo);
44         if (ret)
45                 goto out;
46
47         ret = nouveau_bo_pin(chan->pushbuf_bo, mem);
48         if (ret)
49                 goto out;
50
51         ret = nouveau_bo_map(chan->pushbuf_bo);
52         if (ret)
53                 goto out;
54
55         /* create DMA object covering the entire memtype where the push
56          * buffer resides, userspace can submit its own push buffers from
57          * anywhere within the same memtype.
58          */
59         chan->pushbuf_base = chan->pushbuf_bo->bo.offset;
60         if (dev_priv->card_type >= NV_50) {
61                 ret = nouveau_bo_vma_add(chan->pushbuf_bo, chan->vm,
62                                          &chan->pushbuf_vma);
63                 if (ret)
64                         goto out;
65
66                 if (dev_priv->card_type < NV_C0) {
67                         ret = nouveau_gpuobj_dma_new(chan,
68                                                      NV_CLASS_DMA_IN_MEMORY, 0,
69                                                      (1ULL << 40),
70                                                      NV_MEM_ACCESS_RO,
71                                                      NV_MEM_TARGET_VM,
72                                                      &chan->pushbuf);
73                 }
74                 chan->pushbuf_base = chan->pushbuf_vma.offset;
75         } else
76         if (chan->pushbuf_bo->bo.mem.mem_type == TTM_PL_TT) {
77                 ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY, 0,
78                                              dev_priv->gart_info.aper_size,
79                                              NV_MEM_ACCESS_RO,
80                                              NV_MEM_TARGET_GART,
81                                              &chan->pushbuf);
82         } else
83         if (dev_priv->card_type != NV_04) {
84                 ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY, 0,
85                                              dev_priv->fb_available_size,
86                                              NV_MEM_ACCESS_RO,
87                                              NV_MEM_TARGET_VRAM,
88                                              &chan->pushbuf);
89         } else {
90                 /* NV04 cmdbuf hack, from original ddx.. not sure of it's
91                  * exact reason for existing :)  PCI access to cmdbuf in
92                  * VRAM.
93                  */
94                 ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
95                                              pci_resource_start(dev->pdev, 1),
96                                              dev_priv->fb_available_size,
97                                              NV_MEM_ACCESS_RO,
98                                              NV_MEM_TARGET_PCI,
99                                              &chan->pushbuf);
100         }
101
102 out:
103         if (ret) {
104                 NV_ERROR(dev, "error initialising pushbuf: %d\n", ret);
105                 nouveau_bo_vma_del(chan->pushbuf_bo, &chan->pushbuf_vma);
106                 nouveau_gpuobj_ref(NULL, &chan->pushbuf);
107                 if (chan->pushbuf_bo) {
108                         nouveau_bo_unmap(chan->pushbuf_bo);
109                         nouveau_bo_ref(NULL, &chan->pushbuf_bo);
110                 }
111         }
112
113         return 0;
114 }
115
116 /* allocates and initializes a fifo for user space consumption */
117 int
118 nouveau_channel_alloc(struct drm_device *dev, struct nouveau_channel **chan_ret,
119                       struct drm_file *file_priv,
120                       uint32_t vram_handle, uint32_t gart_handle)
121 {
122         struct drm_nouveau_private *dev_priv = dev->dev_private;
123         struct nouveau_fifo_engine *pfifo = &dev_priv->engine.fifo;
124         struct nouveau_fpriv *fpriv = nouveau_fpriv(file_priv);
125         struct nouveau_channel *chan;
126         unsigned long flags;
127         int ret, i;
128
129         /* allocate and lock channel structure */
130         chan = kzalloc(sizeof(*chan), GFP_KERNEL);
131         if (!chan)
132                 return -ENOMEM;
133         chan->dev = dev;
134         chan->file_priv = file_priv;
135         chan->vram_handle = vram_handle;
136         chan->gart_handle = gart_handle;
137
138         kref_init(&chan->ref);
139         atomic_set(&chan->users, 1);
140         mutex_init(&chan->mutex);
141         mutex_lock(&chan->mutex);
142
143         /* allocate hw channel id */
144         spin_lock_irqsave(&dev_priv->channels.lock, flags);
145         for (chan->id = 0; chan->id < pfifo->channels; chan->id++) {
146                 if (!dev_priv->channels.ptr[chan->id]) {
147                         nouveau_channel_ref(chan, &dev_priv->channels.ptr[chan->id]);
148                         break;
149                 }
150         }
151         spin_unlock_irqrestore(&dev_priv->channels.lock, flags);
152
153         if (chan->id == pfifo->channels) {
154                 mutex_unlock(&chan->mutex);
155                 kfree(chan);
156                 return -ENODEV;
157         }
158
159         NV_DEBUG(dev, "initialising channel %d\n", chan->id);
160         INIT_LIST_HEAD(&chan->fence.pending);
161         spin_lock_init(&chan->fence.lock);
162
163         /* setup channel's memory and vm */
164         ret = nouveau_gpuobj_channel_init(chan, vram_handle, gart_handle);
165         if (ret) {
166                 NV_ERROR(dev, "gpuobj %d\n", ret);
167                 nouveau_channel_put(&chan);
168                 return ret;
169         }
170
171         /* Allocate space for per-channel fixed notifier memory */
172         ret = nouveau_notifier_init_channel(chan);
173         if (ret) {
174                 NV_ERROR(dev, "ntfy %d\n", ret);
175                 nouveau_channel_put(&chan);
176                 return ret;
177         }
178
179         /* Allocate DMA push buffer */
180         ret = nouveau_channel_pushbuf_init(chan);
181         if (ret) {
182                 NV_ERROR(dev, "pushbuf %d\n", ret);
183                 nouveau_channel_put(&chan);
184                 return ret;
185         }
186
187         nouveau_dma_init(chan);
188         chan->user_put = 0x40;
189         chan->user_get = 0x44;
190         if (dev_priv->card_type >= NV_50)
191                 chan->user_get_hi = 0x60;
192
193         /* disable the fifo caches */
194         pfifo->reassign(dev, false);
195
196         /* Construct initial RAMFC for new channel */
197         ret = pfifo->create_context(chan);
198         if (ret) {
199                 nouveau_channel_put(&chan);
200                 return ret;
201         }
202
203         pfifo->reassign(dev, true);
204
205         /* Insert NOPs for NOUVEAU_DMA_SKIPS */
206         ret = RING_SPACE(chan, NOUVEAU_DMA_SKIPS);
207         if (ret) {
208                 nouveau_channel_put(&chan);
209                 return ret;
210         }
211
212         for (i = 0; i < NOUVEAU_DMA_SKIPS; i++)
213                 OUT_RING  (chan, 0x00000000);
214         FIRE_RING(chan);
215
216         ret = nouveau_gpuobj_gr_new(chan, NvSw, nouveau_software_class(dev));
217         if (ret) {
218                 nouveau_channel_put(&chan);
219                 return ret;
220         }
221
222         ret = nouveau_fence_channel_init(chan);
223         if (ret) {
224                 nouveau_channel_put(&chan);
225                 return ret;
226         }
227
228         nouveau_debugfs_channel_init(chan);
229
230         NV_DEBUG(dev, "channel %d initialised\n", chan->id);
231         if (fpriv) {
232                 spin_lock(&fpriv->lock);
233                 list_add(&chan->list, &fpriv->channels);
234                 spin_unlock(&fpriv->lock);
235         }
236         *chan_ret = chan;
237         return 0;
238 }
239
240 struct nouveau_channel *
241 nouveau_channel_get_unlocked(struct nouveau_channel *ref)
242 {
243         struct nouveau_channel *chan = NULL;
244
245         if (likely(ref && atomic_inc_not_zero(&ref->users)))
246                 nouveau_channel_ref(ref, &chan);
247
248         return chan;
249 }
250
251 struct nouveau_channel *
252 nouveau_channel_get(struct drm_file *file_priv, int id)
253 {
254         struct nouveau_fpriv *fpriv = nouveau_fpriv(file_priv);
255         struct nouveau_channel *chan;
256
257         spin_lock(&fpriv->lock);
258         list_for_each_entry(chan, &fpriv->channels, list) {
259                 if (chan->id == id) {
260                         chan = nouveau_channel_get_unlocked(chan);
261                         spin_unlock(&fpriv->lock);
262                         mutex_lock(&chan->mutex);
263                         return chan;
264                 }
265         }
266         spin_unlock(&fpriv->lock);
267
268         return ERR_PTR(-EINVAL);
269 }
270
271 void
272 nouveau_channel_put_unlocked(struct nouveau_channel **pchan)
273 {
274         struct nouveau_channel *chan = *pchan;
275         struct drm_device *dev = chan->dev;
276         struct drm_nouveau_private *dev_priv = dev->dev_private;
277         struct nouveau_fifo_engine *pfifo = &dev_priv->engine.fifo;
278         unsigned long flags;
279         int i;
280
281         /* decrement the refcount, and we're done if there's still refs */
282         if (likely(!atomic_dec_and_test(&chan->users))) {
283                 nouveau_channel_ref(NULL, pchan);
284                 return;
285         }
286
287         /* no one wants the channel anymore */
288         NV_DEBUG(dev, "freeing channel %d\n", chan->id);
289         nouveau_debugfs_channel_fini(chan);
290
291         /* give it chance to idle */
292         nouveau_channel_idle(chan);
293
294         /* ensure all outstanding fences are signaled.  they should be if the
295          * above attempts at idling were OK, but if we failed this'll tell TTM
296          * we're done with the buffers.
297          */
298         nouveau_fence_channel_fini(chan);
299
300         /* boot it off the hardware */
301         pfifo->reassign(dev, false);
302
303         /* destroy the engine specific contexts */
304         pfifo->destroy_context(chan);
305         for (i = 0; i < NVOBJ_ENGINE_NR; i++) {
306                 if (chan->engctx[i])
307                         dev_priv->eng[i]->context_del(chan, i);
308         }
309
310         pfifo->reassign(dev, true);
311
312         /* aside from its resources, the channel should now be dead,
313          * remove it from the channel list
314          */
315         spin_lock_irqsave(&dev_priv->channels.lock, flags);
316         nouveau_channel_ref(NULL, &dev_priv->channels.ptr[chan->id]);
317         spin_unlock_irqrestore(&dev_priv->channels.lock, flags);
318
319         /* destroy any resources the channel owned */
320         nouveau_gpuobj_ref(NULL, &chan->pushbuf);
321         if (chan->pushbuf_bo) {
322                 nouveau_bo_vma_del(chan->pushbuf_bo, &chan->pushbuf_vma);
323                 nouveau_bo_unmap(chan->pushbuf_bo);
324                 nouveau_bo_unpin(chan->pushbuf_bo);
325                 nouveau_bo_ref(NULL, &chan->pushbuf_bo);
326         }
327         nouveau_ramht_ref(NULL, &chan->ramht, chan);
328         nouveau_notifier_takedown_channel(chan);
329         nouveau_gpuobj_channel_takedown(chan);
330
331         nouveau_channel_ref(NULL, pchan);
332 }
333
334 void
335 nouveau_channel_put(struct nouveau_channel **pchan)
336 {
337         mutex_unlock(&(*pchan)->mutex);
338         nouveau_channel_put_unlocked(pchan);
339 }
340
341 static void
342 nouveau_channel_del(struct kref *ref)
343 {
344         struct nouveau_channel *chan =
345                 container_of(ref, struct nouveau_channel, ref);
346
347         kfree(chan);
348 }
349
350 void
351 nouveau_channel_ref(struct nouveau_channel *chan,
352                     struct nouveau_channel **pchan)
353 {
354         if (chan)
355                 kref_get(&chan->ref);
356
357         if (*pchan)
358                 kref_put(&(*pchan)->ref, nouveau_channel_del);
359
360         *pchan = chan;
361 }
362
363 void
364 nouveau_channel_idle(struct nouveau_channel *chan)
365 {
366         struct drm_device *dev = chan->dev;
367         struct nouveau_fence *fence = NULL;
368         int ret;
369
370         nouveau_fence_update(chan);
371
372         if (chan->fence.sequence != chan->fence.sequence_ack) {
373                 ret = nouveau_fence_new(chan, &fence);
374                 if (!ret) {
375                         ret = nouveau_fence_wait(fence, false, false);
376                         nouveau_fence_unref(&fence);
377                 }
378
379                 if (ret)
380                         NV_ERROR(dev, "Failed to idle channel %d.\n", chan->id);
381         }
382 }
383
384 /* cleans up all the fifos from file_priv */
385 void
386 nouveau_channel_cleanup(struct drm_device *dev, struct drm_file *file_priv)
387 {
388         struct drm_nouveau_private *dev_priv = dev->dev_private;
389         struct nouveau_engine *engine = &dev_priv->engine;
390         struct nouveau_channel *chan;
391         int i;
392
393         NV_DEBUG(dev, "clearing FIFO enables from file_priv\n");
394         for (i = 0; i < engine->fifo.channels; i++) {
395                 chan = nouveau_channel_get(file_priv, i);
396                 if (IS_ERR(chan))
397                         continue;
398
399                 list_del(&chan->list);
400                 atomic_dec(&chan->users);
401                 nouveau_channel_put(&chan);
402         }
403 }
404
405
406 /***********************************
407  * ioctls wrapping the functions
408  ***********************************/
409
410 static int
411 nouveau_ioctl_fifo_alloc(struct drm_device *dev, void *data,
412                          struct drm_file *file_priv)
413 {
414         struct drm_nouveau_private *dev_priv = dev->dev_private;
415         struct drm_nouveau_channel_alloc *init = data;
416         struct nouveau_channel *chan;
417         int ret;
418
419         if (!dev_priv->eng[NVOBJ_ENGINE_GR])
420                 return -ENODEV;
421
422         if (init->fb_ctxdma_handle == ~0 || init->tt_ctxdma_handle == ~0)
423                 return -EINVAL;
424
425         ret = nouveau_channel_alloc(dev, &chan, file_priv,
426                                     init->fb_ctxdma_handle,
427                                     init->tt_ctxdma_handle);
428         if (ret)
429                 return ret;
430         init->channel  = chan->id;
431
432         if (nouveau_vram_pushbuf == 0) {
433                 if (chan->dma.ib_max)
434                         init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_VRAM |
435                                                 NOUVEAU_GEM_DOMAIN_GART;
436                 else if (chan->pushbuf_bo->bo.mem.mem_type == TTM_PL_VRAM)
437                         init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_VRAM;
438                 else
439                         init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_GART;
440         } else {
441                 init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_VRAM;
442         }
443
444         if (dev_priv->card_type < NV_C0) {
445                 init->subchan[0].handle = 0x00000000;
446                 init->subchan[0].grclass = 0x0000;
447                 init->subchan[1].handle = NvSw;
448                 init->subchan[1].grclass = NV_SW;
449                 init->nr_subchan = 2;
450         }
451
452         /* Named memory object area */
453         ret = drm_gem_handle_create(file_priv, chan->notifier_bo->gem,
454                                     &init->notifier_handle);
455
456         if (ret == 0)
457                 atomic_inc(&chan->users); /* userspace reference */
458         nouveau_channel_put(&chan);
459         return ret;
460 }
461
462 static int
463 nouveau_ioctl_fifo_free(struct drm_device *dev, void *data,
464                         struct drm_file *file_priv)
465 {
466         struct drm_nouveau_channel_free *req = data;
467         struct nouveau_channel *chan;
468
469         chan = nouveau_channel_get(file_priv, req->channel);
470         if (IS_ERR(chan))
471                 return PTR_ERR(chan);
472
473         list_del(&chan->list);
474         atomic_dec(&chan->users);
475         nouveau_channel_put(&chan);
476         return 0;
477 }
478
479 /***********************************
480  * finally, the ioctl table
481  ***********************************/
482
483 struct drm_ioctl_desc nouveau_ioctls[] = {
484         DRM_IOCTL_DEF_DRV(NOUVEAU_GETPARAM, nouveau_ioctl_getparam, DRM_UNLOCKED|DRM_AUTH),
485         DRM_IOCTL_DEF_DRV(NOUVEAU_SETPARAM, nouveau_ioctl_setparam, DRM_UNLOCKED|DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
486         DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_ALLOC, nouveau_ioctl_fifo_alloc, DRM_UNLOCKED|DRM_AUTH),
487         DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_FREE, nouveau_ioctl_fifo_free, DRM_UNLOCKED|DRM_AUTH),
488         DRM_IOCTL_DEF_DRV(NOUVEAU_GROBJ_ALLOC, nouveau_ioctl_grobj_alloc, DRM_UNLOCKED|DRM_AUTH),
489         DRM_IOCTL_DEF_DRV(NOUVEAU_NOTIFIEROBJ_ALLOC, nouveau_ioctl_notifier_alloc, DRM_UNLOCKED|DRM_AUTH),
490         DRM_IOCTL_DEF_DRV(NOUVEAU_GPUOBJ_FREE, nouveau_ioctl_gpuobj_free, DRM_UNLOCKED|DRM_AUTH),
491         DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_NEW, nouveau_gem_ioctl_new, DRM_UNLOCKED|DRM_AUTH),
492         DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_PUSHBUF, nouveau_gem_ioctl_pushbuf, DRM_UNLOCKED|DRM_AUTH),
493         DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_PREP, nouveau_gem_ioctl_cpu_prep, DRM_UNLOCKED|DRM_AUTH),
494         DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_FINI, nouveau_gem_ioctl_cpu_fini, DRM_UNLOCKED|DRM_AUTH),
495         DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_INFO, nouveau_gem_ioctl_info, DRM_UNLOCKED|DRM_AUTH),
496 };
497
498 int nouveau_max_ioctl = DRM_ARRAY_SIZE(nouveau_ioctls);