Add linux-next specific files for 20110831
[linux-2.6/next.git] / drivers / gpu / drm / nouveau / nouveau_channel.c
blobb0d753f45bbd48da0874df192d999f9bcc27a950
1 /*
2 * Copyright 2005-2006 Stephane Marchesin
3 * All Rights Reserved.
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:
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.
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.
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"
32 static int
33 nouveau_channel_pushbuf_init(struct nouveau_channel *chan)
35 u32 mem = nouveau_vram_pushbuf ? TTM_PL_FLAG_VRAM : TTM_PL_FLAG_TT;
36 struct drm_device *dev = chan->dev;
37 struct drm_nouveau_private *dev_priv = dev->dev_private;
38 int ret;
40 /* allocate buffer object */
41 ret = nouveau_bo_new(dev, 65536, 0, mem, 0, 0, &chan->pushbuf_bo);
42 if (ret)
43 goto out;
45 ret = nouveau_bo_pin(chan->pushbuf_bo, mem);
46 if (ret)
47 goto out;
49 ret = nouveau_bo_map(chan->pushbuf_bo);
50 if (ret)
51 goto out;
53 /* create DMA object covering the entire memtype where the push
54 * buffer resides, userspace can submit its own push buffers from
55 * anywhere within the same memtype.
57 chan->pushbuf_base = chan->pushbuf_bo->bo.offset;
58 if (dev_priv->card_type >= NV_50) {
59 ret = nouveau_bo_vma_add(chan->pushbuf_bo, chan->vm,
60 &chan->pushbuf_vma);
61 if (ret)
62 goto out;
64 if (dev_priv->card_type < NV_C0) {
65 ret = nouveau_gpuobj_dma_new(chan,
66 NV_CLASS_DMA_IN_MEMORY, 0,
67 (1ULL << 40),
68 NV_MEM_ACCESS_RO,
69 NV_MEM_TARGET_VM,
70 &chan->pushbuf);
72 chan->pushbuf_base = chan->pushbuf_vma.offset;
73 } else
74 if (chan->pushbuf_bo->bo.mem.mem_type == TTM_PL_TT) {
75 ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY, 0,
76 dev_priv->gart_info.aper_size,
77 NV_MEM_ACCESS_RO,
78 NV_MEM_TARGET_GART,
79 &chan->pushbuf);
80 } else
81 if (dev_priv->card_type != NV_04) {
82 ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY, 0,
83 dev_priv->fb_available_size,
84 NV_MEM_ACCESS_RO,
85 NV_MEM_TARGET_VRAM,
86 &chan->pushbuf);
87 } else {
88 /* NV04 cmdbuf hack, from original ddx.. not sure of it's
89 * exact reason for existing :) PCI access to cmdbuf in
90 * VRAM.
92 ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
93 pci_resource_start(dev->pdev, 1),
94 dev_priv->fb_available_size,
95 NV_MEM_ACCESS_RO,
96 NV_MEM_TARGET_PCI,
97 &chan->pushbuf);
100 out:
101 if (ret) {
102 NV_ERROR(dev, "error initialising pushbuf: %d\n", ret);
103 nouveau_bo_vma_del(chan->pushbuf_bo, &chan->pushbuf_vma);
104 nouveau_gpuobj_ref(NULL, &chan->pushbuf);
105 if (chan->pushbuf_bo) {
106 nouveau_bo_unmap(chan->pushbuf_bo);
107 nouveau_bo_ref(NULL, &chan->pushbuf_bo);
111 return 0;
114 /* allocates and initializes a fifo for user space consumption */
116 nouveau_channel_alloc(struct drm_device *dev, struct nouveau_channel **chan_ret,
117 struct drm_file *file_priv,
118 uint32_t vram_handle, uint32_t gart_handle)
120 struct drm_nouveau_private *dev_priv = dev->dev_private;
121 struct nouveau_fifo_engine *pfifo = &dev_priv->engine.fifo;
122 struct nouveau_fpriv *fpriv = nouveau_fpriv(file_priv);
123 struct nouveau_channel *chan;
124 unsigned long flags;
125 int ret;
127 /* allocate and lock channel structure */
128 chan = kzalloc(sizeof(*chan), GFP_KERNEL);
129 if (!chan)
130 return -ENOMEM;
131 chan->dev = dev;
132 chan->file_priv = file_priv;
133 chan->vram_handle = vram_handle;
134 chan->gart_handle = gart_handle;
136 kref_init(&chan->ref);
137 atomic_set(&chan->users, 1);
138 mutex_init(&chan->mutex);
139 mutex_lock(&chan->mutex);
141 /* allocate hw channel id */
142 spin_lock_irqsave(&dev_priv->channels.lock, flags);
143 for (chan->id = 0; chan->id < pfifo->channels; chan->id++) {
144 if (!dev_priv->channels.ptr[chan->id]) {
145 nouveau_channel_ref(chan, &dev_priv->channels.ptr[chan->id]);
146 break;
149 spin_unlock_irqrestore(&dev_priv->channels.lock, flags);
151 if (chan->id == pfifo->channels) {
152 mutex_unlock(&chan->mutex);
153 kfree(chan);
154 return -ENODEV;
157 NV_DEBUG(dev, "initialising channel %d\n", chan->id);
158 INIT_LIST_HEAD(&chan->nvsw.vbl_wait);
159 INIT_LIST_HEAD(&chan->nvsw.flip);
160 INIT_LIST_HEAD(&chan->fence.pending);
162 /* setup channel's memory and vm */
163 ret = nouveau_gpuobj_channel_init(chan, vram_handle, gart_handle);
164 if (ret) {
165 NV_ERROR(dev, "gpuobj %d\n", ret);
166 nouveau_channel_put(&chan);
167 return ret;
170 /* Allocate space for per-channel fixed notifier memory */
171 ret = nouveau_notifier_init_channel(chan);
172 if (ret) {
173 NV_ERROR(dev, "ntfy %d\n", ret);
174 nouveau_channel_put(&chan);
175 return ret;
178 /* Allocate DMA push buffer */
179 ret = nouveau_channel_pushbuf_init(chan);
180 if (ret) {
181 NV_ERROR(dev, "pushbuf %d\n", ret);
182 nouveau_channel_put(&chan);
183 return ret;
186 nouveau_dma_pre_init(chan);
187 chan->user_put = 0x40;
188 chan->user_get = 0x44;
190 /* disable the fifo caches */
191 pfifo->reassign(dev, false);
193 /* Construct initial RAMFC for new channel */
194 ret = pfifo->create_context(chan);
195 if (ret) {
196 nouveau_channel_put(&chan);
197 return ret;
200 pfifo->reassign(dev, true);
202 ret = nouveau_dma_init(chan);
203 if (!ret)
204 ret = nouveau_fence_channel_init(chan);
205 if (ret) {
206 nouveau_channel_put(&chan);
207 return ret;
210 nouveau_debugfs_channel_init(chan);
212 NV_DEBUG(dev, "channel %d initialised\n", chan->id);
213 if (fpriv) {
214 spin_lock(&fpriv->lock);
215 list_add(&chan->list, &fpriv->channels);
216 spin_unlock(&fpriv->lock);
218 *chan_ret = chan;
219 return 0;
222 struct nouveau_channel *
223 nouveau_channel_get_unlocked(struct nouveau_channel *ref)
225 struct nouveau_channel *chan = NULL;
227 if (likely(ref && atomic_inc_not_zero(&ref->users)))
228 nouveau_channel_ref(ref, &chan);
230 return chan;
233 struct nouveau_channel *
234 nouveau_channel_get(struct drm_file *file_priv, int id)
236 struct nouveau_fpriv *fpriv = nouveau_fpriv(file_priv);
237 struct nouveau_channel *chan;
239 spin_lock(&fpriv->lock);
240 list_for_each_entry(chan, &fpriv->channels, list) {
241 if (chan->id == id) {
242 chan = nouveau_channel_get_unlocked(chan);
243 spin_unlock(&fpriv->lock);
244 mutex_lock(&chan->mutex);
245 return chan;
248 spin_unlock(&fpriv->lock);
250 return ERR_PTR(-EINVAL);
253 void
254 nouveau_channel_put_unlocked(struct nouveau_channel **pchan)
256 struct nouveau_channel *chan = *pchan;
257 struct drm_device *dev = chan->dev;
258 struct drm_nouveau_private *dev_priv = dev->dev_private;
259 struct nouveau_fifo_engine *pfifo = &dev_priv->engine.fifo;
260 unsigned long flags;
261 int i;
263 /* decrement the refcount, and we're done if there's still refs */
264 if (likely(!atomic_dec_and_test(&chan->users))) {
265 nouveau_channel_ref(NULL, pchan);
266 return;
269 /* no one wants the channel anymore */
270 NV_DEBUG(dev, "freeing channel %d\n", chan->id);
271 nouveau_debugfs_channel_fini(chan);
273 /* give it chance to idle */
274 nouveau_channel_idle(chan);
276 /* ensure all outstanding fences are signaled. they should be if the
277 * above attempts at idling were OK, but if we failed this'll tell TTM
278 * we're done with the buffers.
280 nouveau_fence_channel_fini(chan);
282 /* boot it off the hardware */
283 pfifo->reassign(dev, false);
285 /* destroy the engine specific contexts */
286 pfifo->destroy_context(chan);
287 for (i = 0; i < NVOBJ_ENGINE_NR; i++) {
288 if (chan->engctx[i])
289 dev_priv->eng[i]->context_del(chan, i);
292 pfifo->reassign(dev, true);
294 /* aside from its resources, the channel should now be dead,
295 * remove it from the channel list
297 spin_lock_irqsave(&dev_priv->channels.lock, flags);
298 nouveau_channel_ref(NULL, &dev_priv->channels.ptr[chan->id]);
299 spin_unlock_irqrestore(&dev_priv->channels.lock, flags);
301 /* destroy any resources the channel owned */
302 nouveau_gpuobj_ref(NULL, &chan->pushbuf);
303 if (chan->pushbuf_bo) {
304 nouveau_bo_vma_del(chan->pushbuf_bo, &chan->pushbuf_vma);
305 nouveau_bo_unmap(chan->pushbuf_bo);
306 nouveau_bo_unpin(chan->pushbuf_bo);
307 nouveau_bo_ref(NULL, &chan->pushbuf_bo);
309 nouveau_ramht_ref(NULL, &chan->ramht, chan);
310 nouveau_notifier_takedown_channel(chan);
311 nouveau_gpuobj_channel_takedown(chan);
313 nouveau_channel_ref(NULL, pchan);
316 void
317 nouveau_channel_put(struct nouveau_channel **pchan)
319 mutex_unlock(&(*pchan)->mutex);
320 nouveau_channel_put_unlocked(pchan);
323 static void
324 nouveau_channel_del(struct kref *ref)
326 struct nouveau_channel *chan =
327 container_of(ref, struct nouveau_channel, ref);
329 kfree(chan);
332 void
333 nouveau_channel_ref(struct nouveau_channel *chan,
334 struct nouveau_channel **pchan)
336 if (chan)
337 kref_get(&chan->ref);
339 if (*pchan)
340 kref_put(&(*pchan)->ref, nouveau_channel_del);
342 *pchan = chan;
345 void
346 nouveau_channel_idle(struct nouveau_channel *chan)
348 struct drm_device *dev = chan->dev;
349 struct nouveau_fence *fence = NULL;
350 int ret;
352 nouveau_fence_update(chan);
354 if (chan->fence.sequence != chan->fence.sequence_ack) {
355 ret = nouveau_fence_new(chan, &fence, true);
356 if (!ret) {
357 ret = nouveau_fence_wait(fence, false, false);
358 nouveau_fence_unref(&fence);
361 if (ret)
362 NV_ERROR(dev, "Failed to idle channel %d.\n", chan->id);
366 /* cleans up all the fifos from file_priv */
367 void
368 nouveau_channel_cleanup(struct drm_device *dev, struct drm_file *file_priv)
370 struct drm_nouveau_private *dev_priv = dev->dev_private;
371 struct nouveau_engine *engine = &dev_priv->engine;
372 struct nouveau_channel *chan;
373 int i;
375 NV_DEBUG(dev, "clearing FIFO enables from file_priv\n");
376 for (i = 0; i < engine->fifo.channels; i++) {
377 chan = nouveau_channel_get(file_priv, i);
378 if (IS_ERR(chan))
379 continue;
381 list_del(&chan->list);
382 atomic_dec(&chan->users);
383 nouveau_channel_put(&chan);
388 /***********************************
389 * ioctls wrapping the functions
390 ***********************************/
392 static int
393 nouveau_ioctl_fifo_alloc(struct drm_device *dev, void *data,
394 struct drm_file *file_priv)
396 struct drm_nouveau_private *dev_priv = dev->dev_private;
397 struct drm_nouveau_channel_alloc *init = data;
398 struct nouveau_channel *chan;
399 int ret;
401 if (!dev_priv->eng[NVOBJ_ENGINE_GR])
402 return -ENODEV;
404 if (init->fb_ctxdma_handle == ~0 || init->tt_ctxdma_handle == ~0)
405 return -EINVAL;
407 ret = nouveau_channel_alloc(dev, &chan, file_priv,
408 init->fb_ctxdma_handle,
409 init->tt_ctxdma_handle);
410 if (ret)
411 return ret;
412 init->channel = chan->id;
414 if (chan->dma.ib_max)
415 init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_VRAM |
416 NOUVEAU_GEM_DOMAIN_GART;
417 else if (chan->pushbuf_bo->bo.mem.mem_type == TTM_PL_VRAM)
418 init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_VRAM;
419 else
420 init->pushbuf_domains = NOUVEAU_GEM_DOMAIN_GART;
422 if (dev_priv->card_type < NV_C0) {
423 init->subchan[0].handle = NvM2MF;
424 if (dev_priv->card_type < NV_50)
425 init->subchan[0].grclass = 0x0039;
426 else
427 init->subchan[0].grclass = 0x5039;
428 init->subchan[1].handle = NvSw;
429 init->subchan[1].grclass = NV_SW;
430 init->nr_subchan = 2;
431 } else {
432 init->subchan[0].handle = 0x9039;
433 init->subchan[0].grclass = 0x9039;
434 init->nr_subchan = 1;
437 /* Named memory object area */
438 ret = drm_gem_handle_create(file_priv, chan->notifier_bo->gem,
439 &init->notifier_handle);
441 if (ret == 0)
442 atomic_inc(&chan->users); /* userspace reference */
443 nouveau_channel_put(&chan);
444 return ret;
447 static int
448 nouveau_ioctl_fifo_free(struct drm_device *dev, void *data,
449 struct drm_file *file_priv)
451 struct drm_nouveau_channel_free *req = data;
452 struct nouveau_channel *chan;
454 chan = nouveau_channel_get(file_priv, req->channel);
455 if (IS_ERR(chan))
456 return PTR_ERR(chan);
458 list_del(&chan->list);
459 atomic_dec(&chan->users);
460 nouveau_channel_put(&chan);
461 return 0;
464 /***********************************
465 * finally, the ioctl table
466 ***********************************/
468 struct drm_ioctl_desc nouveau_ioctls[] = {
469 DRM_IOCTL_DEF_DRV(NOUVEAU_GETPARAM, nouveau_ioctl_getparam, DRM_UNLOCKED|DRM_AUTH),
470 DRM_IOCTL_DEF_DRV(NOUVEAU_SETPARAM, nouveau_ioctl_setparam, DRM_UNLOCKED|DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
471 DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_ALLOC, nouveau_ioctl_fifo_alloc, DRM_UNLOCKED|DRM_AUTH),
472 DRM_IOCTL_DEF_DRV(NOUVEAU_CHANNEL_FREE, nouveau_ioctl_fifo_free, DRM_UNLOCKED|DRM_AUTH),
473 DRM_IOCTL_DEF_DRV(NOUVEAU_GROBJ_ALLOC, nouveau_ioctl_grobj_alloc, DRM_UNLOCKED|DRM_AUTH),
474 DRM_IOCTL_DEF_DRV(NOUVEAU_NOTIFIEROBJ_ALLOC, nouveau_ioctl_notifier_alloc, DRM_UNLOCKED|DRM_AUTH),
475 DRM_IOCTL_DEF_DRV(NOUVEAU_GPUOBJ_FREE, nouveau_ioctl_gpuobj_free, DRM_UNLOCKED|DRM_AUTH),
476 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_NEW, nouveau_gem_ioctl_new, DRM_UNLOCKED|DRM_AUTH),
477 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_PUSHBUF, nouveau_gem_ioctl_pushbuf, DRM_UNLOCKED|DRM_AUTH),
478 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_PREP, nouveau_gem_ioctl_cpu_prep, DRM_UNLOCKED|DRM_AUTH),
479 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_FINI, nouveau_gem_ioctl_cpu_fini, DRM_UNLOCKED|DRM_AUTH),
480 DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_INFO, nouveau_gem_ioctl_info, DRM_UNLOCKED|DRM_AUTH),
483 int nouveau_max_ioctl = DRM_ARRAY_SIZE(nouveau_ioctls);