nouveau_fence.c 14.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*
 * Copyright (C) 2007 Ben Skeggs.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */

#include "drmP.h"
#include "drm.h"

30 31 32
#include <linux/ktime.h>
#include <linux/hrtimer.h>

33
#include "nouveau_drv.h"
34
#include "nouveau_ramht.h"
35
#include "nouveau_software.h"
36 37
#include "nouveau_dma.h"

38
#define USE_REFCNT(dev) (nouveau_private(dev)->chipset >= 0x10)
39
#define USE_SEMA(dev) (nouveau_private(dev)->chipset >= 0x17)
40 41 42 43 44 45 46 47

struct nouveau_fence {
	struct nouveau_channel *channel;
	struct kref refcount;
	struct list_head entry;

	uint32_t sequence;
	bool signalled;
48
	unsigned long timeout;
49 50 51

	void (*work)(void *priv, bool signalled);
	void *priv;
52 53
};

54 55 56 57 58 59
struct nouveau_semaphore {
	struct kref ref;
	struct drm_device *dev;
	struct drm_mm_node *mem;
};

60 61 62 63 64 65 66 67 68 69 70 71
static inline struct nouveau_fence *
nouveau_fence(void *sync_obj)
{
	return (struct nouveau_fence *)sync_obj;
}

static void
nouveau_fence_del(struct kref *ref)
{
	struct nouveau_fence *fence =
		container_of(ref, struct nouveau_fence, refcount);

72
	nouveau_channel_ref(NULL, &fence->channel);
73 74 75 76 77 78
	kfree(fence);
}

void
nouveau_fence_update(struct nouveau_channel *chan)
{
79 80
	struct drm_device *dev = chan->dev;
	struct nouveau_fence *tmp, *fence;
81 82
	uint32_t sequence;

83 84
	spin_lock(&chan->fence.lock);

85 86 87 88 89 90 91 92 93 94 95
	/* Fetch the last sequence if the channel is still up and running */
	if (likely(!list_empty(&chan->fence.pending))) {
		if (USE_REFCNT(dev))
			sequence = nvchan_rd32(chan, 0x48);
		else
			sequence = atomic_read(&chan->fence.last_sequence_irq);

		if (chan->fence.sequence_ack == sequence)
			goto out;
		chan->fence.sequence_ack = sequence;
	}
96

97
	list_for_each_entry_safe(fence, tmp, &chan->fence.pending, entry) {
98 99 100
		if (fence->sequence > chan->fence.sequence_ack)
			break;

101 102
		fence->signalled = true;
		list_del(&fence->entry);
103
		if (fence->work)
104 105
			fence->work(fence->priv, true);

106 107
		kref_put(&fence->refcount, nouveau_fence_del);
	}
108

109
out:
110
	spin_unlock(&chan->fence.lock);
111 112 113 114 115 116 117 118 119 120 121 122 123
}

int
nouveau_fence_new(struct nouveau_channel *chan, struct nouveau_fence **pfence,
		  bool emit)
{
	struct nouveau_fence *fence;
	int ret = 0;

	fence = kzalloc(sizeof(*fence), GFP_KERNEL);
	if (!fence)
		return -ENOMEM;
	kref_init(&fence->refcount);
124
	nouveau_channel_ref(chan, &fence->channel);
125 126 127 128 129

	if (emit)
		ret = nouveau_fence_emit(fence);

	if (ret)
130
		nouveau_fence_unref(&fence);
131 132 133 134 135 136 137
	*pfence = fence;
	return ret;
}

struct nouveau_channel *
nouveau_fence_channel(struct nouveau_fence *fence)
{
138
	return fence ? nouveau_channel_get_unlocked(fence->channel) : NULL;
139 140 141 142 143 144
}

int
nouveau_fence_emit(struct nouveau_fence *fence)
{
	struct nouveau_channel *chan = fence->channel;
145
	struct drm_device *dev = chan->dev;
B
Ben Skeggs 已提交
146
	struct drm_nouveau_private *dev_priv = dev->dev_private;
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
	int ret;

	ret = RING_SPACE(chan, 2);
	if (ret)
		return ret;

	if (unlikely(chan->fence.sequence == chan->fence.sequence_ack - 1)) {
		nouveau_fence_update(chan);

		BUG_ON(chan->fence.sequence ==
		       chan->fence.sequence_ack - 1);
	}

	fence->sequence = ++chan->fence.sequence;

	kref_get(&fence->refcount);
163
	spin_lock(&chan->fence.lock);
164
	list_add_tail(&fence->entry, &chan->fence.pending);
165
	spin_unlock(&chan->fence.lock);
166

B
Ben Skeggs 已提交
167 168
	if (USE_REFCNT(dev)) {
		if (dev_priv->card_type < NV_C0)
169
			BEGIN_NV04(chan, 0, NV10_SUBCHAN_REF_CNT, 1);
B
Ben Skeggs 已提交
170
		else
171
			BEGIN_NVC0(chan, 0, NV10_SUBCHAN_REF_CNT, 1);
B
Ben Skeggs 已提交
172
	} else {
173
		BEGIN_NV04(chan, NvSubSw, 0x0150, 1);
B
Ben Skeggs 已提交
174 175
	}
	OUT_RING (chan, fence->sequence);
176
	FIRE_RING(chan);
177
	fence->timeout = jiffies + 3 * DRM_HZ;
178 179 180 181

	return 0;
}

182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
void
nouveau_fence_work(struct nouveau_fence *fence,
		   void (*work)(void *priv, bool signalled),
		   void *priv)
{
	BUG_ON(fence->work);

	spin_lock(&fence->channel->fence.lock);

	if (fence->signalled) {
		work(priv, true);
	} else {
		fence->work = work;
		fence->priv = priv;
	}

	spin_unlock(&fence->channel->fence.lock);
}

201
void
202
__nouveau_fence_unref(void **sync_obj)
203 204 205 206 207 208 209 210 211
{
	struct nouveau_fence *fence = nouveau_fence(*sync_obj);

	if (fence)
		kref_put(&fence->refcount, nouveau_fence_del);
	*sync_obj = NULL;
}

void *
212
__nouveau_fence_ref(void *sync_obj)
213 214 215 216 217 218 219 220
{
	struct nouveau_fence *fence = nouveau_fence(sync_obj);

	kref_get(&fence->refcount);
	return sync_obj;
}

bool
221
__nouveau_fence_signalled(void *sync_obj, void *sync_arg)
222 223 224 225 226 227 228 229 230 231 232 233
{
	struct nouveau_fence *fence = nouveau_fence(sync_obj);
	struct nouveau_channel *chan = fence->channel;

	if (fence->signalled)
		return true;

	nouveau_fence_update(chan);
	return fence->signalled;
}

int
234
__nouveau_fence_wait(void *sync_obj, void *sync_arg, bool lazy, bool intr)
235
{
236 237
	struct nouveau_fence *fence = nouveau_fence(sync_obj);
	unsigned long timeout = fence->timeout;
238 239
	unsigned long sleep_time = NSEC_PER_MSEC / 1000;
	ktime_t t;
240 241 242
	int ret = 0;

	while (1) {
243
		if (__nouveau_fence_signalled(sync_obj, sync_arg))
244 245 246 247 248 249 250
			break;

		if (time_after_eq(jiffies, timeout)) {
			ret = -EBUSY;
			break;
		}

251 252
		__set_current_state(intr ? TASK_INTERRUPTIBLE
			: TASK_UNINTERRUPTIBLE);
253 254 255 256 257 258 259
		if (lazy) {
			t = ktime_set(0, sleep_time);
			schedule_hrtimeout(&t, HRTIMER_MODE_REL);
			sleep_time *= 2;
			if (sleep_time > NSEC_PER_MSEC)
				sleep_time = NSEC_PER_MSEC;
		}
260 261

		if (intr && signal_pending(current)) {
B
Ben Skeggs 已提交
262
			ret = -ERESTARTSYS;
263 264 265 266 267 268 269 270 271
			break;
		}
	}

	__set_current_state(TASK_RUNNING);

	return ret;
}

272
static struct nouveau_semaphore *
273
semaphore_alloc(struct drm_device *dev)
274 275 276
{
	struct drm_nouveau_private *dev_priv = dev->dev_private;
	struct nouveau_semaphore *sema;
277 278
	int size = (dev_priv->chipset < 0x84) ? 4 : 16;
	int ret, i;
279 280 281 282 283 284 285 286

	if (!USE_SEMA(dev))
		return NULL;

	sema = kmalloc(sizeof(*sema), GFP_KERNEL);
	if (!sema)
		goto fail;

287 288 289 290
	ret = drm_mm_pre_get(&dev_priv->fence.heap);
	if (ret)
		goto fail;

291
	spin_lock(&dev_priv->fence.lock);
292
	sema->mem = drm_mm_search_free(&dev_priv->fence.heap, size, 0, 0);
293
	if (sema->mem)
294
		sema->mem = drm_mm_get_block_atomic(sema->mem, size, 0);
295 296 297 298 299 300 301
	spin_unlock(&dev_priv->fence.lock);

	if (!sema->mem)
		goto fail;

	kref_init(&sema->ref);
	sema->dev = dev;
302 303
	for (i = sema->mem->start; i < sema->mem->start + size; i += 4)
		nouveau_bo_wr32(dev_priv->fence.bo, i / 4, 0);
304 305 306 307 308 309 310 311

	return sema;
fail:
	kfree(sema);
	return NULL;
}

static void
312
semaphore_free(struct kref *ref)
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
{
	struct nouveau_semaphore *sema =
		container_of(ref, struct nouveau_semaphore, ref);
	struct drm_nouveau_private *dev_priv = sema->dev->dev_private;

	spin_lock(&dev_priv->fence.lock);
	drm_mm_put_block(sema->mem);
	spin_unlock(&dev_priv->fence.lock);

	kfree(sema);
}

static void
semaphore_work(void *priv, bool signalled)
{
	struct nouveau_semaphore *sema = priv;
	struct drm_nouveau_private *dev_priv = sema->dev->dev_private;

	if (unlikely(!signalled))
		nouveau_bo_wr32(dev_priv->fence.bo, sema->mem->start / 4, 1);

334
	kref_put(&sema->ref, semaphore_free);
335 336 337
}

static int
338
semaphore_acquire(struct nouveau_channel *chan, struct nouveau_semaphore *sema)
339
{
340 341
	struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
	struct nouveau_fence *fence = NULL;
342
	u64 offset = chan->fence.vma.offset + sema->mem->start;
343 344
	int ret;

345
	if (dev_priv->chipset < 0x84) {
346
		ret = RING_SPACE(chan, 4);
347 348
		if (ret)
			return ret;
349

350
		BEGIN_NV04(chan, 0, NV11_SUBCHAN_DMA_SEMAPHORE, 3);
351
		OUT_RING  (chan, NvSema);
352
		OUT_RING  (chan, offset);
353
		OUT_RING  (chan, 1);
354 355
	} else
	if (dev_priv->chipset < 0xc0) {
356
		ret = RING_SPACE(chan, 7);
357 358 359
		if (ret)
			return ret;

360
		BEGIN_NV04(chan, 0, NV11_SUBCHAN_DMA_SEMAPHORE, 1);
361
		OUT_RING  (chan, chan->vram_handle);
362
		BEGIN_NV04(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
363 364
		OUT_RING  (chan, upper_32_bits(offset));
		OUT_RING  (chan, lower_32_bits(offset));
365 366
		OUT_RING  (chan, 1);
		OUT_RING  (chan, 1); /* ACQUIRE_EQ */
367 368 369 370 371
	} else {
		ret = RING_SPACE(chan, 5);
		if (ret)
			return ret;

372
		BEGIN_NVC0(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
373 374 375 376
		OUT_RING  (chan, upper_32_bits(offset));
		OUT_RING  (chan, lower_32_bits(offset));
		OUT_RING  (chan, 1);
		OUT_RING  (chan, 0x1001); /* ACQUIRE_EQ */
377 378
	}

379 380 381 382
	/* Delay semaphore destruction until its work is done */
	ret = nouveau_fence_new(chan, &fence, true);
	if (ret)
		return ret;
383

384 385 386 387 388 389 390 391 392 393 394
	kref_get(&sema->ref);
	nouveau_fence_work(fence, semaphore_work, sema);
	nouveau_fence_unref(&fence);
	return 0;
}

static int
semaphore_release(struct nouveau_channel *chan, struct nouveau_semaphore *sema)
{
	struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
	struct nouveau_fence *fence = NULL;
395
	u64 offset = chan->fence.vma.offset + sema->mem->start;
396 397 398
	int ret;

	if (dev_priv->chipset < 0x84) {
399
		ret = RING_SPACE(chan, 5);
400 401 402
		if (ret)
			return ret;

403
		BEGIN_NV04(chan, 0, NV11_SUBCHAN_DMA_SEMAPHORE, 2);
404
		OUT_RING  (chan, NvSema);
405
		OUT_RING  (chan, offset);
406
		BEGIN_NV04(chan, 0, NV11_SUBCHAN_SEMAPHORE_RELEASE, 1);
407
		OUT_RING  (chan, 1);
408 409
	} else
	if (dev_priv->chipset < 0xc0) {
410
		ret = RING_SPACE(chan, 7);
411 412 413
		if (ret)
			return ret;

414
		BEGIN_NV04(chan, 0, NV11_SUBCHAN_DMA_SEMAPHORE, 1);
415
		OUT_RING  (chan, chan->vram_handle);
416
		BEGIN_NV04(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
417 418
		OUT_RING  (chan, upper_32_bits(offset));
		OUT_RING  (chan, lower_32_bits(offset));
419 420
		OUT_RING  (chan, 1);
		OUT_RING  (chan, 2); /* RELEASE */
421 422 423 424 425
	} else {
		ret = RING_SPACE(chan, 5);
		if (ret)
			return ret;

426
		BEGIN_NVC0(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4);
427 428 429 430
		OUT_RING  (chan, upper_32_bits(offset));
		OUT_RING  (chan, lower_32_bits(offset));
		OUT_RING  (chan, 1);
		OUT_RING  (chan, 0x1002); /* RELEASE */
431 432
	}

433 434 435 436 437 438 439
	/* Delay semaphore destruction until its work is done */
	ret = nouveau_fence_new(chan, &fence, true);
	if (ret)
		return ret;

	kref_get(&sema->ref);
	nouveau_fence_work(fence, semaphore_work, sema);
440
	nouveau_fence_unref(&fence);
441 442 443
	return 0;
}

444 445 446 447 448
int
nouveau_fence_sync(struct nouveau_fence *fence,
		   struct nouveau_channel *wchan)
{
	struct nouveau_channel *chan = nouveau_fence_channel(fence);
449 450
	struct drm_device *dev = wchan->dev;
	struct nouveau_semaphore *sema;
451
	int ret = 0;
452

453
	if (likely(!chan || chan == wchan ||
454
		   nouveau_fence_signalled(fence)))
455
		goto out;
456

457
	sema = semaphore_alloc(dev);
458 459 460
	if (!sema) {
		/* Early card or broken userspace, fall back to
		 * software sync. */
461
		ret = nouveau_fence_wait(fence, true, false);
462
		goto out;
463 464
	}

465
	/* try to take chan's mutex, if we can't take it right away
466 467 468
	 * we have to fallback to software sync to prevent locking
	 * order issues
	 */
469
	if (!mutex_trylock(&chan->mutex)) {
470
		ret = nouveau_fence_wait(fence, true, false);
471
		goto out_unref;
472 473
	}

474
	/* Make wchan wait until it gets signalled */
475
	ret = semaphore_acquire(wchan, sema);
476
	if (ret)
477
		goto out_unlock;
478

479
	/* Signal the semaphore from chan */
480
	ret = semaphore_release(chan, sema);
481 482

out_unlock:
483
	mutex_unlock(&chan->mutex);
484
out_unref:
485
	kref_put(&sema->ref, semaphore_free);
486 487 488
out:
	if (chan)
		nouveau_channel_put_unlocked(&chan);
489
	return ret;
490 491
}

492
int
493
__nouveau_fence_flush(void *sync_obj, void *sync_arg)
494 495 496 497 498
{
	return 0;
}

int
499
nouveau_fence_channel_init(struct nouveau_channel *chan)
500
{
501 502
	struct drm_device *dev = chan->dev;
	struct drm_nouveau_private *dev_priv = dev->dev_private;
503 504 505
	struct nouveau_gpuobj *obj = NULL;
	int ret;

506 507 508 509
	if (dev_priv->card_type < NV_C0) {
		ret = RING_SPACE(chan, 2);
		if (ret)
			return ret;
510

511
		BEGIN_NV04(chan, NvSubSw, NV01_SUBCHAN_OBJECT, 1);
512 513 514
		OUT_RING  (chan, NvSw);
		FIRE_RING (chan);
	}
515

516
	/* Setup area of memory shared between all channels for x-chan sync */
517
	if (USE_SEMA(dev) && dev_priv->chipset < 0x84) {
518
		struct ttm_mem_reg *mem = &dev_priv->fence.bo->bo.mem;
519

520
		ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_FROM_MEMORY,
521
					     mem->start << PAGE_SHIFT,
522
					     mem->size, NV_MEM_ACCESS_RW,
523
					     NV_MEM_TARGET_VRAM, &obj);
524 525 526 527 528 529 530
		if (ret)
			return ret;

		ret = nouveau_ramht_insert(chan, NvSema, obj);
		nouveau_gpuobj_ref(NULL, &obj);
		if (ret)
			return ret;
531 532
	} else
	if (USE_SEMA(dev)) {
533 534 535 536 537
		/* map fence bo into channel's vm */
		ret = nouveau_bo_vma_add(dev_priv->fence.bo, chan->vm,
					 &chan->fence.vma);
		if (ret)
			return ret;
538 539
	}

540
	atomic_set(&chan->fence.last_sequence_irq, 0);
541 542 543 544
	return 0;
}

void
545
nouveau_fence_channel_fini(struct nouveau_channel *chan)
546
{
547
	struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
548
	struct nouveau_fence *tmp, *fence;
549

550
	spin_lock(&chan->fence.lock);
551
	list_for_each_entry_safe(fence, tmp, &chan->fence.pending, entry) {
552 553
		fence->signalled = true;
		list_del(&fence->entry);
554 555 556 557

		if (unlikely(fence->work))
			fence->work(fence->priv, false);

558 559
		kref_put(&fence->refcount, nouveau_fence_del);
	}
560
	spin_unlock(&chan->fence.lock);
561 562

	nouveau_bo_vma_del(dev_priv->fence.bo, &chan->fence.vma);
563 564
}

565 566 567 568
int
nouveau_fence_init(struct drm_device *dev)
{
	struct drm_nouveau_private *dev_priv = dev->dev_private;
569
	int size = (dev_priv->chipset < 0x84) ? 4096 : 16384;
570 571 572 573
	int ret;

	/* Create a shared VRAM heap for cross-channel sync. */
	if (USE_SEMA(dev)) {
574
		ret = nouveau_bo_new(dev, size, 0, TTM_PL_FLAG_VRAM,
D
Dave Airlie 已提交
575
				     0, 0, NULL, &dev_priv->fence.bo);
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
		if (ret)
			return ret;

		ret = nouveau_bo_pin(dev_priv->fence.bo, TTM_PL_FLAG_VRAM);
		if (ret)
			goto fail;

		ret = nouveau_bo_map(dev_priv->fence.bo);
		if (ret)
			goto fail;

		ret = drm_mm_init(&dev_priv->fence.heap, 0,
				  dev_priv->fence.bo->bo.mem.size);
		if (ret)
			goto fail;

		spin_lock_init(&dev_priv->fence.lock);
	}

	return 0;
fail:
	nouveau_bo_unmap(dev_priv->fence.bo);
	nouveau_bo_ref(NULL, &dev_priv->fence.bo);
	return ret;
}

void
nouveau_fence_fini(struct drm_device *dev)
{
	struct drm_nouveau_private *dev_priv = dev->dev_private;

	if (USE_SEMA(dev)) {
		drm_mm_takedown(&dev_priv->fence.heap);
		nouveau_bo_unmap(dev_priv->fence.bo);
		nouveau_bo_unpin(dev_priv->fence.bo);
		nouveau_bo_ref(NULL, &dev_priv->fence.bo);
	}
}