nouveau_fence.c 12.4 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 30
/*
 * 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"

#include "nouveau_drv.h"
31
#include "nouveau_ramht.h"
32 33
#include "nouveau_dma.h"

34
#define USE_REFCNT(dev) (nouveau_private(dev)->chipset >= 0x10)
35
#define USE_SEMA(dev) (nouveau_private(dev)->chipset >= 0x17)
36 37 38 39 40 41 42 43

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

	uint32_t sequence;
	bool signalled;
44 45 46

	void (*work)(void *priv, bool signalled);
	void *priv;
47 48
};

49 50 51 52 53 54
struct nouveau_semaphore {
	struct kref ref;
	struct drm_device *dev;
	struct drm_mm_node *mem;
};

55 56 57 58 59 60 61 62 63 64 65 66
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);

67
	nouveau_channel_ref(NULL, &fence->channel);
68 69 70 71 72 73
	kfree(fence);
}

void
nouveau_fence_update(struct nouveau_channel *chan)
{
74 75
	struct drm_device *dev = chan->dev;
	struct nouveau_fence *tmp, *fence;
76 77
	uint32_t sequence;

78 79
	spin_lock(&chan->fence.lock);

80
	if (USE_REFCNT(dev))
81 82
		sequence = nvchan_rd32(chan, 0x48);
	else
83
		sequence = atomic_read(&chan->fence.last_sequence_irq);
84 85

	if (chan->fence.sequence_ack == sequence)
86
		goto out;
87 88
	chan->fence.sequence_ack = sequence;

89
	list_for_each_entry_safe(fence, tmp, &chan->fence.pending, entry) {
90 91 92
		sequence = fence->sequence;
		fence->signalled = true;
		list_del(&fence->entry);
93 94 95 96

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

97 98 99 100 101
		kref_put(&fence->refcount, nouveau_fence_del);

		if (sequence == chan->fence.sequence_ack)
			break;
	}
102
out:
103
	spin_unlock(&chan->fence.lock);
104 105 106 107 108 109 110 111 112 113 114 115 116
}

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);
117
	nouveau_channel_ref(chan, &fence->channel);
118 119 120 121 122

	if (emit)
		ret = nouveau_fence_emit(fence);

	if (ret)
123
		nouveau_fence_unref(&fence);
124 125 126 127 128 129 130
	*pfence = fence;
	return ret;
}

struct nouveau_channel *
nouveau_fence_channel(struct nouveau_fence *fence)
{
131
	return fence ? nouveau_channel_get_unlocked(fence->channel) : NULL;
132 133 134 135 136 137
}

int
nouveau_fence_emit(struct nouveau_fence *fence)
{
	struct nouveau_channel *chan = fence->channel;
138
	struct drm_device *dev = chan->dev;
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
	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);
155
	spin_lock(&chan->fence.lock);
156
	list_add_tail(&fence->entry, &chan->fence.pending);
157
	spin_unlock(&chan->fence.lock);
158

159
	BEGIN_RING(chan, NvSubSw, USE_REFCNT(dev) ? 0x0050 : 0x0150, 1);
160 161 162 163 164 165
	OUT_RING(chan, fence->sequence);
	FIRE_RING(chan);

	return 0;
}

166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
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);
}

185
void
186
__nouveau_fence_unref(void **sync_obj)
187 188 189 190 191 192 193 194 195
{
	struct nouveau_fence *fence = nouveau_fence(*sync_obj);

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

void *
196
__nouveau_fence_ref(void *sync_obj)
197 198 199 200 201 202 203 204
{
	struct nouveau_fence *fence = nouveau_fence(sync_obj);

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

bool
205
__nouveau_fence_signalled(void *sync_obj, void *sync_arg)
206 207 208 209 210 211 212 213 214 215 216 217
{
	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
218
__nouveau_fence_wait(void *sync_obj, void *sync_arg, bool lazy, bool intr)
219 220 221 222 223
{
	unsigned long timeout = jiffies + (3 * DRM_HZ);
	int ret = 0;

	while (1) {
224
		if (__nouveau_fence_signalled(sync_obj, sync_arg))
225 226 227 228 229 230 231
			break;

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

232 233
		__set_current_state(intr ? TASK_INTERRUPTIBLE
			: TASK_UNINTERRUPTIBLE);
234 235 236 237
		if (lazy)
			schedule_timeout(1);

		if (intr && signal_pending(current)) {
B
Ben Skeggs 已提交
238
			ret = -ERESTARTSYS;
239 240 241 242 243 244 245 246 247
			break;
		}
	}

	__set_current_state(TASK_RUNNING);

	return ret;
}

248 249 250 251 252
static struct nouveau_semaphore *
alloc_semaphore(struct drm_device *dev)
{
	struct drm_nouveau_private *dev_priv = dev->dev_private;
	struct nouveau_semaphore *sema;
253
	int ret;
254 255 256 257 258 259 260 261

	if (!USE_SEMA(dev))
		return NULL;

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

262 263 264 265
	ret = drm_mm_pre_get(&dev_priv->fence.heap);
	if (ret)
		goto fail;

266 267 268
	spin_lock(&dev_priv->fence.lock);
	sema->mem = drm_mm_search_free(&dev_priv->fence.heap, 4, 0, 0);
	if (sema->mem)
269
		sema->mem = drm_mm_get_block_atomic(sema->mem, 4, 0);
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
	spin_unlock(&dev_priv->fence.lock);

	if (!sema->mem)
		goto fail;

	kref_init(&sema->ref);
	sema->dev = dev;
	nouveau_bo_wr32(dev_priv->fence.bo, sema->mem->start / 4, 0);

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

static void
free_semaphore(struct kref *ref)
{
	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);

	kref_put(&sema->ref, free_semaphore);
}

static int
emit_semaphore(struct nouveau_channel *chan, int method,
	       struct nouveau_semaphore *sema)
{
	struct drm_nouveau_private *dev_priv = sema->dev->dev_private;
	struct nouveau_fence *fence;
317
	bool smart = (dev_priv->card_type >= NV_50);
318 319
	int ret;

320
	ret = RING_SPACE(chan, smart ? 8 : 4);
321 322 323
	if (ret)
		return ret;

324
	if (smart) {
325 326 327 328 329
		BEGIN_RING(chan, NvSubSw, NV_SW_DMA_SEMAPHORE, 1);
		OUT_RING(chan, NvSema);
	}
	BEGIN_RING(chan, NvSubSw, NV_SW_SEMAPHORE_OFFSET, 1);
	OUT_RING(chan, sema->mem->start);
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351

	if (smart && method == NV_SW_SEMAPHORE_ACQUIRE) {
		/*
		 * NV50 tries to be too smart and context-switch
		 * between semaphores instead of doing a "first come,
		 * first served" strategy like previous cards
		 * do.
		 *
		 * That's bad because the ACQUIRE latency can get as
		 * large as the PFIFO context time slice in the
		 * typical DRI2 case where you have several
		 * outstanding semaphores at the same moment.
		 *
		 * If we're going to ACQUIRE, force the card to
		 * context switch before, just in case the matching
		 * RELEASE is already scheduled to be executed in
		 * another channel.
		 */
		BEGIN_RING(chan, NvSubSw, NV_SW_YIELD, 1);
		OUT_RING(chan, 0);
	}

352 353 354
	BEGIN_RING(chan, NvSubSw, method, 1);
	OUT_RING(chan, 1);

355 356 357 358 359 360 361 362 363 364
	if (smart && method == NV_SW_SEMAPHORE_RELEASE) {
		/*
		 * Force the card to context switch, there may be
		 * another channel waiting for the semaphore we just
		 * released.
		 */
		BEGIN_RING(chan, NvSubSw, NV_SW_YIELD, 1);
		OUT_RING(chan, 0);
	}

365 366 367 368 369 370 371
	/* 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);
372
	nouveau_fence_unref(&fence);
373 374 375 376

	return 0;
}

377 378 379 380 381
int
nouveau_fence_sync(struct nouveau_fence *fence,
		   struct nouveau_channel *wchan)
{
	struct nouveau_channel *chan = nouveau_fence_channel(fence);
382 383
	struct drm_device *dev = wchan->dev;
	struct nouveau_semaphore *sema;
384
	int ret = 0;
385

386
	if (likely(!chan || chan == wchan ||
387
		   nouveau_fence_signalled(fence)))
388
		goto out;
389

390 391 392 393
	sema = alloc_semaphore(dev);
	if (!sema) {
		/* Early card or broken userspace, fall back to
		 * software sync. */
394
		ret = nouveau_fence_wait(fence, true, false);
395
		goto out;
396 397
	}

398
	/* try to take chan's mutex, if we can't take it right away
399 400 401
	 * we have to fallback to software sync to prevent locking
	 * order issues
	 */
402
	if (!mutex_trylock(&chan->mutex)) {
403
		ret = nouveau_fence_wait(fence, true, false);
404
		goto out_unref;
405 406
	}

407 408
	/* Make wchan wait until it gets signalled */
	ret = emit_semaphore(wchan, NV_SW_SEMAPHORE_ACQUIRE, sema);
409
	if (ret)
410
		goto out_unlock;
411

412 413
	/* Signal the semaphore from chan */
	ret = emit_semaphore(chan, NV_SW_SEMAPHORE_RELEASE, sema);
414 415

out_unlock:
416
	mutex_unlock(&chan->mutex);
417
out_unref:
418
	kref_put(&sema->ref, free_semaphore);
419 420 421
out:
	if (chan)
		nouveau_channel_put_unlocked(&chan);
422
	return ret;
423 424
}

425
int
426
__nouveau_fence_flush(void *sync_obj, void *sync_arg)
427 428 429 430 431
{
	return 0;
}

int
432
nouveau_fence_channel_init(struct nouveau_channel *chan)
433
{
434 435
	struct drm_device *dev = chan->dev;
	struct drm_nouveau_private *dev_priv = dev->dev_private;
436 437 438 439
	struct nouveau_gpuobj *obj = NULL;
	int ret;

	/* Create an NV_SW object for various sync purposes */
440
	ret = nouveau_gpuobj_gr_new(chan, NV_SW, &obj);
441 442 443 444 445 446 447 448 449 450 451 452 453 454
	if (ret)
		return ret;

	ret = nouveau_ramht_insert(chan, NvSw, obj);
	nouveau_gpuobj_ref(NULL, &obj);
	if (ret)
		return ret;

	ret = RING_SPACE(chan, 2);
	if (ret)
		return ret;
	BEGIN_RING(chan, NvSubSw, 0, 1);
	OUT_RING(chan, NvSw);

455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
	/* Create a DMA object for the shared cross-channel sync area. */
	if (USE_SEMA(dev)) {
		struct drm_mm_node *mem = dev_priv->fence.bo->bo.mem.mm_node;

		ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
					     mem->start << PAGE_SHIFT,
					     mem->size << PAGE_SHIFT,
					     NV_DMA_ACCESS_RW,
					     NV_DMA_TARGET_VIDMEM, &obj);
		if (ret)
			return ret;

		ret = nouveau_ramht_insert(chan, NvSema, obj);
		nouveau_gpuobj_ref(NULL, &obj);
		if (ret)
			return ret;

		ret = RING_SPACE(chan, 2);
		if (ret)
			return ret;
		BEGIN_RING(chan, NvSubSw, NV_SW_DMA_SEMAPHORE, 1);
		OUT_RING(chan, NvSema);
	}

479 480
	FIRE_RING(chan);

481 482
	INIT_LIST_HEAD(&chan->fence.pending);
	spin_lock_init(&chan->fence.lock);
483
	atomic_set(&chan->fence.last_sequence_irq, 0);
484

485 486 487 488
	return 0;
}

void
489
nouveau_fence_channel_fini(struct nouveau_channel *chan)
490
{
491
	struct nouveau_fence *tmp, *fence;
492

493 494
	spin_lock(&chan->fence.lock);

495
	list_for_each_entry_safe(fence, tmp, &chan->fence.pending, entry) {
496 497
		fence->signalled = true;
		list_del(&fence->entry);
498 499 500 501

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

502 503
		kref_put(&fence->refcount, nouveau_fence_del);
	}
504 505

	spin_unlock(&chan->fence.lock);
506 507
}

508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
int
nouveau_fence_init(struct drm_device *dev)
{
	struct drm_nouveau_private *dev_priv = dev->dev_private;
	int ret;

	/* Create a shared VRAM heap for cross-channel sync. */
	if (USE_SEMA(dev)) {
		ret = nouveau_bo_new(dev, NULL, 4096, 0, TTM_PL_FLAG_VRAM,
				     0, 0, false, true, &dev_priv->fence.bo);
		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);
	}
}