blob: 2579fc69d18233d4725f284e2f79e52239500990 [file] [log] [blame]
Ben Skeggs6ee73862009-12-11 19:24:15 +10001/*
2 * Copyright (C) 2007 Ben Skeggs.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 */
26
27#include "drmP.h"
28#include "drm.h"
29
30#include "nouveau_drv.h"
Francisco Jerez0c6c1c22010-09-22 00:58:54 +020031#include "nouveau_ramht.h"
Ben Skeggs6ee73862009-12-11 19:24:15 +100032#include "nouveau_dma.h"
33
Francisco Jerez27307232010-09-21 18:57:11 +020034#define USE_REFCNT(dev) (nouveau_private(dev)->chipset >= 0x10)
Francisco Jerez0c6c1c22010-09-22 00:58:54 +020035#define USE_SEMA(dev) (nouveau_private(dev)->chipset >= 0x17)
Ben Skeggs6ee73862009-12-11 19:24:15 +100036
37struct nouveau_fence {
38 struct nouveau_channel *channel;
39 struct kref refcount;
40 struct list_head entry;
41
42 uint32_t sequence;
43 bool signalled;
Francisco Jerez8ac38912010-09-21 20:49:39 +020044
45 void (*work)(void *priv, bool signalled);
46 void *priv;
Ben Skeggs6ee73862009-12-11 19:24:15 +100047};
48
Francisco Jerez0c6c1c22010-09-22 00:58:54 +020049struct nouveau_semaphore {
50 struct kref ref;
51 struct drm_device *dev;
52 struct drm_mm_node *mem;
53};
54
Ben Skeggs6ee73862009-12-11 19:24:15 +100055static inline struct nouveau_fence *
56nouveau_fence(void *sync_obj)
57{
58 return (struct nouveau_fence *)sync_obj;
59}
60
61static void
62nouveau_fence_del(struct kref *ref)
63{
64 struct nouveau_fence *fence =
65 container_of(ref, struct nouveau_fence, refcount);
66
Francisco Jerez2a6789a2010-10-18 03:56:14 +020067 nouveau_channel_ref(NULL, &fence->channel);
Ben Skeggs6ee73862009-12-11 19:24:15 +100068 kfree(fence);
69}
70
71void
72nouveau_fence_update(struct nouveau_channel *chan)
73{
Francisco Jerez27307232010-09-21 18:57:11 +020074 struct drm_device *dev = chan->dev;
75 struct nouveau_fence *tmp, *fence;
Ben Skeggs6ee73862009-12-11 19:24:15 +100076 uint32_t sequence;
77
Francisco Jerez3ba64622010-08-28 17:56:33 +020078 spin_lock(&chan->fence.lock);
79
Francisco Jerez27307232010-09-21 18:57:11 +020080 if (USE_REFCNT(dev))
Ben Skeggs6ee73862009-12-11 19:24:15 +100081 sequence = nvchan_rd32(chan, 0x48);
82 else
Ben Skeggs047d1d32010-05-31 12:00:43 +100083 sequence = atomic_read(&chan->fence.last_sequence_irq);
Ben Skeggs6ee73862009-12-11 19:24:15 +100084
85 if (chan->fence.sequence_ack == sequence)
Francisco Jerez3ba64622010-08-28 17:56:33 +020086 goto out;
Ben Skeggs6ee73862009-12-11 19:24:15 +100087 chan->fence.sequence_ack = sequence;
88
Francisco Jerez27307232010-09-21 18:57:11 +020089 list_for_each_entry_safe(fence, tmp, &chan->fence.pending, entry) {
Ben Skeggs6ee73862009-12-11 19:24:15 +100090 sequence = fence->sequence;
91 fence->signalled = true;
92 list_del(&fence->entry);
Francisco Jerez8ac38912010-09-21 20:49:39 +020093
94 if (unlikely(fence->work))
95 fence->work(fence->priv, true);
96
Ben Skeggs6ee73862009-12-11 19:24:15 +100097 kref_put(&fence->refcount, nouveau_fence_del);
98
99 if (sequence == chan->fence.sequence_ack)
100 break;
101 }
Francisco Jerez3ba64622010-08-28 17:56:33 +0200102out:
Ben Skeggs047d1d32010-05-31 12:00:43 +1000103 spin_unlock(&chan->fence.lock);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000104}
105
106int
107nouveau_fence_new(struct nouveau_channel *chan, struct nouveau_fence **pfence,
108 bool emit)
109{
110 struct nouveau_fence *fence;
111 int ret = 0;
112
113 fence = kzalloc(sizeof(*fence), GFP_KERNEL);
114 if (!fence)
115 return -ENOMEM;
116 kref_init(&fence->refcount);
Francisco Jerez2a6789a2010-10-18 03:56:14 +0200117 nouveau_channel_ref(chan, &fence->channel);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000118
119 if (emit)
120 ret = nouveau_fence_emit(fence);
121
122 if (ret)
Marcin Slusarz382d62e2010-10-20 21:50:24 +0200123 nouveau_fence_unref(&fence);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000124 *pfence = fence;
125 return ret;
126}
127
128struct nouveau_channel *
129nouveau_fence_channel(struct nouveau_fence *fence)
130{
Francisco Jerez2b478ad2010-10-18 03:56:40 +0200131 return fence ? nouveau_channel_get_unlocked(fence->channel) : NULL;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000132}
133
134int
135nouveau_fence_emit(struct nouveau_fence *fence)
136{
Ben Skeggs6ee73862009-12-11 19:24:15 +1000137 struct nouveau_channel *chan = fence->channel;
Francisco Jerez27307232010-09-21 18:57:11 +0200138 struct drm_device *dev = chan->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000139 int ret;
140
141 ret = RING_SPACE(chan, 2);
142 if (ret)
143 return ret;
144
145 if (unlikely(chan->fence.sequence == chan->fence.sequence_ack - 1)) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000146 nouveau_fence_update(chan);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000147
148 BUG_ON(chan->fence.sequence ==
149 chan->fence.sequence_ack - 1);
150 }
151
152 fence->sequence = ++chan->fence.sequence;
153
154 kref_get(&fence->refcount);
Ben Skeggs047d1d32010-05-31 12:00:43 +1000155 spin_lock(&chan->fence.lock);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000156 list_add_tail(&fence->entry, &chan->fence.pending);
Ben Skeggs047d1d32010-05-31 12:00:43 +1000157 spin_unlock(&chan->fence.lock);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000158
Francisco Jerez27307232010-09-21 18:57:11 +0200159 BEGIN_RING(chan, NvSubSw, USE_REFCNT(dev) ? 0x0050 : 0x0150, 1);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000160 OUT_RING(chan, fence->sequence);
161 FIRE_RING(chan);
162
163 return 0;
164}
165
166void
Francisco Jerez8ac38912010-09-21 20:49:39 +0200167nouveau_fence_work(struct nouveau_fence *fence,
168 void (*work)(void *priv, bool signalled),
169 void *priv)
170{
171 BUG_ON(fence->work);
172
173 spin_lock(&fence->channel->fence.lock);
174
175 if (fence->signalled) {
176 work(priv, true);
177 } else {
178 fence->work = work;
179 fence->priv = priv;
180 }
181
182 spin_unlock(&fence->channel->fence.lock);
183}
184
185void
Marcin Slusarz382d62e2010-10-20 21:50:24 +0200186__nouveau_fence_unref(void **sync_obj)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000187{
188 struct nouveau_fence *fence = nouveau_fence(*sync_obj);
189
190 if (fence)
191 kref_put(&fence->refcount, nouveau_fence_del);
192 *sync_obj = NULL;
193}
194
195void *
Marcin Slusarz382d62e2010-10-20 21:50:24 +0200196__nouveau_fence_ref(void *sync_obj)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000197{
198 struct nouveau_fence *fence = nouveau_fence(sync_obj);
199
200 kref_get(&fence->refcount);
201 return sync_obj;
202}
203
204bool
Marcin Slusarz382d62e2010-10-20 21:50:24 +0200205__nouveau_fence_signalled(void *sync_obj, void *sync_arg)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000206{
207 struct nouveau_fence *fence = nouveau_fence(sync_obj);
208 struct nouveau_channel *chan = fence->channel;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000209
210 if (fence->signalled)
211 return true;
212
Ben Skeggs6ee73862009-12-11 19:24:15 +1000213 nouveau_fence_update(chan);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000214 return fence->signalled;
215}
216
217int
Marcin Slusarz382d62e2010-10-20 21:50:24 +0200218__nouveau_fence_wait(void *sync_obj, void *sync_arg, bool lazy, bool intr)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000219{
220 unsigned long timeout = jiffies + (3 * DRM_HZ);
221 int ret = 0;
222
Ben Skeggs6ee73862009-12-11 19:24:15 +1000223 while (1) {
Marcin Slusarz382d62e2010-10-20 21:50:24 +0200224 if (__nouveau_fence_signalled(sync_obj, sync_arg))
Ben Skeggs6ee73862009-12-11 19:24:15 +1000225 break;
226
227 if (time_after_eq(jiffies, timeout)) {
228 ret = -EBUSY;
229 break;
230 }
231
Kulikov Vasiliy05991112010-07-26 12:23:54 +0400232 __set_current_state(intr ? TASK_INTERRUPTIBLE
233 : TASK_UNINTERRUPTIBLE);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000234 if (lazy)
235 schedule_timeout(1);
236
237 if (intr && signal_pending(current)) {
Ben Skeggs9ddc8c52009-12-15 11:04:25 +1000238 ret = -ERESTARTSYS;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000239 break;
240 }
241 }
242
243 __set_current_state(TASK_RUNNING);
244
245 return ret;
246}
247
Francisco Jerez0c6c1c22010-09-22 00:58:54 +0200248static struct nouveau_semaphore *
249alloc_semaphore(struct drm_device *dev)
250{
251 struct drm_nouveau_private *dev_priv = dev->dev_private;
252 struct nouveau_semaphore *sema;
Francisco Jerez907af602010-10-09 04:02:09 +0200253 int ret;
Francisco Jerez0c6c1c22010-09-22 00:58:54 +0200254
255 if (!USE_SEMA(dev))
256 return NULL;
257
258 sema = kmalloc(sizeof(*sema), GFP_KERNEL);
259 if (!sema)
260 goto fail;
261
Francisco Jerez907af602010-10-09 04:02:09 +0200262 ret = drm_mm_pre_get(&dev_priv->fence.heap);
263 if (ret)
264 goto fail;
265
Francisco Jerez0c6c1c22010-09-22 00:58:54 +0200266 spin_lock(&dev_priv->fence.lock);
267 sema->mem = drm_mm_search_free(&dev_priv->fence.heap, 4, 0, 0);
268 if (sema->mem)
Francisco Jerez907af602010-10-09 04:02:09 +0200269 sema->mem = drm_mm_get_block_atomic(sema->mem, 4, 0);
Francisco Jerez0c6c1c22010-09-22 00:58:54 +0200270 spin_unlock(&dev_priv->fence.lock);
271
272 if (!sema->mem)
273 goto fail;
274
275 kref_init(&sema->ref);
276 sema->dev = dev;
277 nouveau_bo_wr32(dev_priv->fence.bo, sema->mem->start / 4, 0);
278
279 return sema;
280fail:
281 kfree(sema);
282 return NULL;
283}
284
285static void
286free_semaphore(struct kref *ref)
287{
288 struct nouveau_semaphore *sema =
289 container_of(ref, struct nouveau_semaphore, ref);
290 struct drm_nouveau_private *dev_priv = sema->dev->dev_private;
291
292 spin_lock(&dev_priv->fence.lock);
293 drm_mm_put_block(sema->mem);
294 spin_unlock(&dev_priv->fence.lock);
295
296 kfree(sema);
297}
298
299static void
300semaphore_work(void *priv, bool signalled)
301{
302 struct nouveau_semaphore *sema = priv;
303 struct drm_nouveau_private *dev_priv = sema->dev->dev_private;
304
305 if (unlikely(!signalled))
306 nouveau_bo_wr32(dev_priv->fence.bo, sema->mem->start / 4, 1);
307
308 kref_put(&sema->ref, free_semaphore);
309}
310
311static int
312emit_semaphore(struct nouveau_channel *chan, int method,
313 struct nouveau_semaphore *sema)
314{
315 struct drm_nouveau_private *dev_priv = sema->dev->dev_private;
316 struct nouveau_fence *fence;
Francisco Jerez8af29cc2010-10-02 17:04:46 +0200317 bool smart = (dev_priv->card_type >= NV_50);
Francisco Jerez0c6c1c22010-09-22 00:58:54 +0200318 int ret;
319
Francisco Jerez8af29cc2010-10-02 17:04:46 +0200320 ret = RING_SPACE(chan, smart ? 8 : 4);
Francisco Jerez0c6c1c22010-09-22 00:58:54 +0200321 if (ret)
322 return ret;
323
Francisco Jerez8af29cc2010-10-02 17:04:46 +0200324 if (smart) {
Francisco Jerez0c6c1c22010-09-22 00:58:54 +0200325 BEGIN_RING(chan, NvSubSw, NV_SW_DMA_SEMAPHORE, 1);
326 OUT_RING(chan, NvSema);
327 }
328 BEGIN_RING(chan, NvSubSw, NV_SW_SEMAPHORE_OFFSET, 1);
329 OUT_RING(chan, sema->mem->start);
Francisco Jerez8af29cc2010-10-02 17:04:46 +0200330
331 if (smart && method == NV_SW_SEMAPHORE_ACQUIRE) {
332 /*
333 * NV50 tries to be too smart and context-switch
334 * between semaphores instead of doing a "first come,
335 * first served" strategy like previous cards
336 * do.
337 *
338 * That's bad because the ACQUIRE latency can get as
339 * large as the PFIFO context time slice in the
340 * typical DRI2 case where you have several
341 * outstanding semaphores at the same moment.
342 *
343 * If we're going to ACQUIRE, force the card to
344 * context switch before, just in case the matching
345 * RELEASE is already scheduled to be executed in
346 * another channel.
347 */
348 BEGIN_RING(chan, NvSubSw, NV_SW_YIELD, 1);
349 OUT_RING(chan, 0);
350 }
351
Francisco Jerez0c6c1c22010-09-22 00:58:54 +0200352 BEGIN_RING(chan, NvSubSw, method, 1);
353 OUT_RING(chan, 1);
354
Francisco Jerez8af29cc2010-10-02 17:04:46 +0200355 if (smart && method == NV_SW_SEMAPHORE_RELEASE) {
356 /*
357 * Force the card to context switch, there may be
358 * another channel waiting for the semaphore we just
359 * released.
360 */
361 BEGIN_RING(chan, NvSubSw, NV_SW_YIELD, 1);
362 OUT_RING(chan, 0);
363 }
364
Francisco Jerez0c6c1c22010-09-22 00:58:54 +0200365 /* Delay semaphore destruction until its work is done */
366 ret = nouveau_fence_new(chan, &fence, true);
367 if (ret)
368 return ret;
369
370 kref_get(&sema->ref);
371 nouveau_fence_work(fence, semaphore_work, sema);
Marcin Slusarz382d62e2010-10-20 21:50:24 +0200372 nouveau_fence_unref(&fence);
Francisco Jerez0c6c1c22010-09-22 00:58:54 +0200373
374 return 0;
375}
376
Ben Skeggs6ee73862009-12-11 19:24:15 +1000377int
Francisco Jerez27307232010-09-21 18:57:11 +0200378nouveau_fence_sync(struct nouveau_fence *fence,
379 struct nouveau_channel *wchan)
380{
381 struct nouveau_channel *chan = nouveau_fence_channel(fence);
Francisco Jerez0c6c1c22010-09-22 00:58:54 +0200382 struct drm_device *dev = wchan->dev;
383 struct nouveau_semaphore *sema;
Francisco Jerez2b478ad2010-10-18 03:56:40 +0200384 int ret = 0;
Francisco Jerez27307232010-09-21 18:57:11 +0200385
Francisco Jerez2b478ad2010-10-18 03:56:40 +0200386 if (likely(!chan || chan == wchan ||
Marcin Slusarz382d62e2010-10-20 21:50:24 +0200387 nouveau_fence_signalled(fence)))
Francisco Jerez2b478ad2010-10-18 03:56:40 +0200388 goto out;
Francisco Jerez27307232010-09-21 18:57:11 +0200389
Francisco Jerez0c6c1c22010-09-22 00:58:54 +0200390 sema = alloc_semaphore(dev);
391 if (!sema) {
392 /* Early card or broken userspace, fall back to
393 * software sync. */
Marcin Slusarz382d62e2010-10-20 21:50:24 +0200394 ret = nouveau_fence_wait(fence, true, false);
Francisco Jerez2b478ad2010-10-18 03:56:40 +0200395 goto out;
Francisco Jerez0c6c1c22010-09-22 00:58:54 +0200396 }
397
Ben Skeggs08cd3d42010-10-12 08:01:59 +1000398 /* try to take chan's mutex, if we can't take it right away
Ben Skeggscff5c132010-10-06 16:16:59 +1000399 * we have to fallback to software sync to prevent locking
400 * order issues
401 */
Ben Skeggs08cd3d42010-10-12 08:01:59 +1000402 if (!mutex_trylock(&chan->mutex)) {
Marcin Slusarz382d62e2010-10-20 21:50:24 +0200403 ret = nouveau_fence_wait(fence, true, false);
Francisco Jerez2b478ad2010-10-18 03:56:40 +0200404 goto out_unref;
Ben Skeggscff5c132010-10-06 16:16:59 +1000405 }
406
Francisco Jerez8af29cc2010-10-02 17:04:46 +0200407 /* Make wchan wait until it gets signalled */
408 ret = emit_semaphore(wchan, NV_SW_SEMAPHORE_ACQUIRE, sema);
Francisco Jerez0c6c1c22010-09-22 00:58:54 +0200409 if (ret)
Francisco Jerez2b478ad2010-10-18 03:56:40 +0200410 goto out_unlock;
Francisco Jerez0c6c1c22010-09-22 00:58:54 +0200411
Francisco Jerez8af29cc2010-10-02 17:04:46 +0200412 /* Signal the semaphore from chan */
413 ret = emit_semaphore(chan, NV_SW_SEMAPHORE_RELEASE, sema);
Francisco Jerez2b478ad2010-10-18 03:56:40 +0200414
415out_unlock:
Ben Skeggs08cd3d42010-10-12 08:01:59 +1000416 mutex_unlock(&chan->mutex);
Francisco Jerez2b478ad2010-10-18 03:56:40 +0200417out_unref:
Francisco Jerez0c6c1c22010-09-22 00:58:54 +0200418 kref_put(&sema->ref, free_semaphore);
Francisco Jerez2b478ad2010-10-18 03:56:40 +0200419out:
420 if (chan)
421 nouveau_channel_put_unlocked(&chan);
Francisco Jerez0c6c1c22010-09-22 00:58:54 +0200422 return ret;
Francisco Jerez27307232010-09-21 18:57:11 +0200423}
424
425int
Marcin Slusarz382d62e2010-10-20 21:50:24 +0200426__nouveau_fence_flush(void *sync_obj, void *sync_arg)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000427{
428 return 0;
429}
430
Ben Skeggs6ee73862009-12-11 19:24:15 +1000431int
Francisco Jerez27307232010-09-21 18:57:11 +0200432nouveau_fence_channel_init(struct nouveau_channel *chan)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000433{
Francisco Jerez0c6c1c22010-09-22 00:58:54 +0200434 struct drm_device *dev = chan->dev;
435 struct drm_nouveau_private *dev_priv = dev->dev_private;
Francisco Jerez27307232010-09-21 18:57:11 +0200436 struct nouveau_gpuobj *obj = NULL;
437 int ret;
438
439 /* Create an NV_SW object for various sync purposes */
Ben Skeggsa6a1a382010-10-19 19:57:34 +1000440 ret = nouveau_gpuobj_gr_new(chan, NV_SW, &obj);
Francisco Jerez27307232010-09-21 18:57:11 +0200441 if (ret)
442 return ret;
443
444 ret = nouveau_ramht_insert(chan, NvSw, obj);
445 nouveau_gpuobj_ref(NULL, &obj);
446 if (ret)
447 return ret;
448
449 ret = RING_SPACE(chan, 2);
450 if (ret)
451 return ret;
452 BEGIN_RING(chan, NvSubSw, 0, 1);
453 OUT_RING(chan, NvSw);
454
Francisco Jerez0c6c1c22010-09-22 00:58:54 +0200455 /* Create a DMA object for the shared cross-channel sync area. */
456 if (USE_SEMA(dev)) {
457 struct drm_mm_node *mem = dev_priv->fence.bo->bo.mem.mm_node;
458
459 ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
460 mem->start << PAGE_SHIFT,
461 mem->size << PAGE_SHIFT,
Ben Skeggs7f4a1952010-11-16 11:50:09 +1000462 NV_MEM_ACCESS_RW,
463 NV_MEM_TARGET_VRAM, &obj);
Francisco Jerez0c6c1c22010-09-22 00:58:54 +0200464 if (ret)
465 return ret;
466
467 ret = nouveau_ramht_insert(chan, NvSema, obj);
468 nouveau_gpuobj_ref(NULL, &obj);
469 if (ret)
470 return ret;
471
472 ret = RING_SPACE(chan, 2);
473 if (ret)
474 return ret;
475 BEGIN_RING(chan, NvSubSw, NV_SW_DMA_SEMAPHORE, 1);
476 OUT_RING(chan, NvSema);
477 }
478
Francisco Jerez27307232010-09-21 18:57:11 +0200479 FIRE_RING(chan);
480
Ben Skeggs6ee73862009-12-11 19:24:15 +1000481 INIT_LIST_HEAD(&chan->fence.pending);
482 spin_lock_init(&chan->fence.lock);
Ben Skeggs047d1d32010-05-31 12:00:43 +1000483 atomic_set(&chan->fence.last_sequence_irq, 0);
Francisco Jerez27307232010-09-21 18:57:11 +0200484
Ben Skeggs6ee73862009-12-11 19:24:15 +1000485 return 0;
486}
487
488void
Francisco Jerez27307232010-09-21 18:57:11 +0200489nouveau_fence_channel_fini(struct nouveau_channel *chan)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000490{
Francisco Jerez27307232010-09-21 18:57:11 +0200491 struct nouveau_fence *tmp, *fence;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000492
Francisco Jerez889fa932010-10-18 03:57:19 +0200493 spin_lock(&chan->fence.lock);
494
Francisco Jerez27307232010-09-21 18:57:11 +0200495 list_for_each_entry_safe(fence, tmp, &chan->fence.pending, entry) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000496 fence->signalled = true;
497 list_del(&fence->entry);
Francisco Jerez8ac38912010-09-21 20:49:39 +0200498
499 if (unlikely(fence->work))
500 fence->work(fence->priv, false);
501
Ben Skeggs6ee73862009-12-11 19:24:15 +1000502 kref_put(&fence->refcount, nouveau_fence_del);
503 }
Francisco Jerez889fa932010-10-18 03:57:19 +0200504
505 spin_unlock(&chan->fence.lock);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000506}
507
Francisco Jerez0c6c1c22010-09-22 00:58:54 +0200508int
509nouveau_fence_init(struct drm_device *dev)
510{
511 struct drm_nouveau_private *dev_priv = dev->dev_private;
512 int ret;
513
514 /* Create a shared VRAM heap for cross-channel sync. */
515 if (USE_SEMA(dev)) {
516 ret = nouveau_bo_new(dev, NULL, 4096, 0, TTM_PL_FLAG_VRAM,
517 0, 0, false, true, &dev_priv->fence.bo);
518 if (ret)
519 return ret;
520
521 ret = nouveau_bo_pin(dev_priv->fence.bo, TTM_PL_FLAG_VRAM);
522 if (ret)
523 goto fail;
524
525 ret = nouveau_bo_map(dev_priv->fence.bo);
526 if (ret)
527 goto fail;
528
529 ret = drm_mm_init(&dev_priv->fence.heap, 0,
530 dev_priv->fence.bo->bo.mem.size);
531 if (ret)
532 goto fail;
533
534 spin_lock_init(&dev_priv->fence.lock);
535 }
536
537 return 0;
538fail:
539 nouveau_bo_unmap(dev_priv->fence.bo);
540 nouveau_bo_ref(NULL, &dev_priv->fence.bo);
541 return ret;
542}
543
544void
545nouveau_fence_fini(struct drm_device *dev)
546{
547 struct drm_nouveau_private *dev_priv = dev->dev_private;
548
549 if (USE_SEMA(dev)) {
550 drm_mm_takedown(&dev_priv->fence.heap);
551 nouveau_bo_unmap(dev_priv->fence.bo);
552 nouveau_bo_unpin(dev_priv->fence.bo);
553 nouveau_bo_ref(NULL, &dev_priv->fence.bo);
554 }
555}