amdgpu_fence.c 19.2 KB
Newer Older
A
Alex Deucher 已提交
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
/*
 * Copyright 2009 Jerome Glisse.
 * 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, sub license, 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 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 NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE COPYRIGHT HOLDERS, AUTHORS 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.
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 */
/*
 * Authors:
 *    Jerome Glisse <glisse@freedesktop.org>
 *    Dave Airlie
 */
#include <linux/seq_file.h>
#include <linux/atomic.h>
#include <linux/wait.h>
#include <linux/kref.h>
#include <linux/slab.h>
#include <linux/firmware.h>
#include <drm/drmP.h>
#include "amdgpu.h"
#include "amdgpu_trace.h"

/*
 * Fences
 * Fences mark an event in the GPUs pipeline and are used
 * for GPU/CPU synchronization.  When the fence is written,
 * it is expected that all buffers associated with that fence
 * are no longer in use by the associated ring on the GPU and
 * that the the relevant GPU caches have been flushed.
 */

50
struct amdgpu_fence {
51
	struct dma_fence base;
52 53 54 55 56

	/* RB, DMA, etc. */
	struct amdgpu_ring		*ring;
};

57 58
static struct kmem_cache *amdgpu_fence_slab;

59 60 61 62 63 64 65 66 67 68 69 70
int amdgpu_fence_slab_init(void)
{
	amdgpu_fence_slab = kmem_cache_create(
		"amdgpu_fence", sizeof(struct amdgpu_fence), 0,
		SLAB_HWCACHE_ALIGN, NULL);
	if (!amdgpu_fence_slab)
		return -ENOMEM;
	return 0;
}

void amdgpu_fence_slab_fini(void)
{
71
	rcu_barrier();
72 73
	kmem_cache_destroy(amdgpu_fence_slab);
}
74 75 76
/*
 * Cast helper
 */
77 78
static const struct dma_fence_ops amdgpu_fence_ops;
static inline struct amdgpu_fence *to_amdgpu_fence(struct dma_fence *f)
79 80 81 82 83 84 85 86 87
{
	struct amdgpu_fence *__f = container_of(f, struct amdgpu_fence, base);

	if (__f->base.ops == &amdgpu_fence_ops)
		return __f;

	return NULL;
}

A
Alex Deucher 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
/**
 * amdgpu_fence_write - write a fence value
 *
 * @ring: ring the fence is associated with
 * @seq: sequence number to write
 *
 * Writes a fence value to memory (all asics).
 */
static void amdgpu_fence_write(struct amdgpu_ring *ring, u32 seq)
{
	struct amdgpu_fence_driver *drv = &ring->fence_drv;

	if (drv->cpu_addr)
		*drv->cpu_addr = cpu_to_le32(seq);
}

/**
 * amdgpu_fence_read - read a fence value
 *
 * @ring: ring the fence is associated with
 *
 * Reads a fence value from memory (all asics).
 * Returns the value of the fence read from memory.
 */
static u32 amdgpu_fence_read(struct amdgpu_ring *ring)
{
	struct amdgpu_fence_driver *drv = &ring->fence_drv;
	u32 seq = 0;

	if (drv->cpu_addr)
		seq = le32_to_cpu(*drv->cpu_addr);
	else
120
		seq = atomic_read(&drv->last_seq);
A
Alex Deucher 已提交
121 122 123 124 125 126 127 128

	return seq;
}

/**
 * amdgpu_fence_emit - emit a fence on the requested ring
 *
 * @ring: ring the fence is associated with
129
 * @f: resulting fence object
A
Alex Deucher 已提交
130 131 132 133
 *
 * Emits a fence command on the requested ring (all asics).
 * Returns 0 on success, -ENOMEM on failure.
 */
134
int amdgpu_fence_emit(struct amdgpu_ring *ring, struct dma_fence **f)
A
Alex Deucher 已提交
135 136
{
	struct amdgpu_device *adev = ring->adev;
137
	struct amdgpu_fence *fence;
138
	struct dma_fence *old, **ptr;
139
	uint32_t seq;
A
Alex Deucher 已提交
140

141 142
	fence = kmem_cache_alloc(amdgpu_fence_slab, GFP_KERNEL);
	if (fence == NULL)
A
Alex Deucher 已提交
143
		return -ENOMEM;
144

145
	seq = ++ring->fence_drv.sync_seq;
146
	fence->ring = ring;
147 148 149 150
	dma_fence_init(&fence->base, &amdgpu_fence_ops,
		       &ring->fence_drv.lock,
		       adev->fence_context + ring->idx,
		       seq);
151
	amdgpu_ring_emit_fence(ring, ring->fence_drv.gpu_addr,
152
			       seq, AMDGPU_FENCE_FLAG_INT);
153

154
	ptr = &ring->fence_drv.fences[seq & ring->fence_drv.num_fences_mask];
155 156 157
	/* This function can't be called concurrently anyway, otherwise
	 * emitting the fence would mess up the hardware ring buffer.
	 */
158
	old = rcu_dereference_protected(*ptr, 1);
159
	if (old && !dma_fence_is_signaled(old)) {
160
		DRM_INFO("rcu slot is busy\n");
161
		dma_fence_wait(old, false);
162
	}
163

164
	rcu_assign_pointer(*ptr, dma_fence_get(&fence->base));
165

166
	*f = &fence->base;
167

A
Alex Deucher 已提交
168 169 170
	return 0;
}

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
/**
 * amdgpu_fence_emit_polling - emit a fence on the requeste ring
 *
 * @ring: ring the fence is associated with
 * @s: resulting sequence number
 *
 * Emits a fence command on the requested ring (all asics).
 * Used For polling fence.
 * Returns 0 on success, -ENOMEM on failure.
 */
int amdgpu_fence_emit_polling(struct amdgpu_ring *ring, uint32_t *s)
{
	uint32_t seq;

	if (!s)
		return -EINVAL;

	seq = ++ring->fence_drv.sync_seq;
	amdgpu_ring_emit_fence(ring, ring->fence_drv.gpu_addr,
190
			       seq, 0);
191 192 193 194 195 196

	*s = seq;

	return 0;
}

197 198 199 200 201 202 203 204 205 206 207 208 209
/**
 * amdgpu_fence_schedule_fallback - schedule fallback check
 *
 * @ring: pointer to struct amdgpu_ring
 *
 * Start a timer as fallback to our interrupts.
 */
static void amdgpu_fence_schedule_fallback(struct amdgpu_ring *ring)
{
	mod_timer(&ring->fence_drv.fallback_timer,
		  jiffies + AMDGPU_FENCE_JIFFIES_TIMEOUT);
}

A
Alex Deucher 已提交
210
/**
211
 * amdgpu_fence_process - check for fence activity
A
Alex Deucher 已提交
212 213 214 215
 *
 * @ring: pointer to struct amdgpu_ring
 *
 * Checks the current fence value and calculates the last
216 217
 * signalled fence value. Wakes the fence queue if the
 * sequence number has increased.
A
Alex Deucher 已提交
218
 */
219
void amdgpu_fence_process(struct amdgpu_ring *ring)
A
Alex Deucher 已提交
220
{
221
	struct amdgpu_fence_driver *drv = &ring->fence_drv;
222
	uint32_t seq, last_seq;
223
	int r;
A
Alex Deucher 已提交
224 225

	do {
226
		last_seq = atomic_read(&ring->fence_drv.last_seq);
A
Alex Deucher 已提交
227 228
		seq = amdgpu_fence_read(ring);

229
	} while (atomic_cmpxchg(&drv->last_seq, last_seq, seq) != last_seq);
A
Alex Deucher 已提交
230

231
	if (seq != ring->fence_drv.sync_seq)
232
		amdgpu_fence_schedule_fallback(ring);
A
Alex Deucher 已提交
233

234 235 236
	if (unlikely(seq == last_seq))
		return;

237 238 239
	last_seq &= drv->num_fences_mask;
	seq &= drv->num_fences_mask;

240
	do {
241
		struct dma_fence *fence, **ptr;
242

243 244 245
		++last_seq;
		last_seq &= drv->num_fences_mask;
		ptr = &drv->fences[last_seq];
246 247 248

		/* There is always exactly one thread signaling this fence slot */
		fence = rcu_dereference_protected(*ptr, 1);
249
		RCU_INIT_POINTER(*ptr, NULL);
250

251 252
		if (!fence)
			continue;
253

254
		r = dma_fence_signal(fence);
255
		if (!r)
256
			DMA_FENCE_TRACE(fence, "signaled from irq context\n");
257 258 259
		else
			BUG();

260
		dma_fence_put(fence);
261
	} while (last_seq != seq);
A
Alex Deucher 已提交
262 263 264
}

/**
265
 * amdgpu_fence_fallback - fallback for hardware interrupts
A
Alex Deucher 已提交
266
 *
267
 * @work: delayed work item
A
Alex Deucher 已提交
268
 *
269
 * Checks for fence activity.
A
Alex Deucher 已提交
270
 */
271
static void amdgpu_fence_fallback(struct timer_list *t)
A
Alex Deucher 已提交
272
{
273 274
	struct amdgpu_ring *ring = from_timer(ring, t,
					      fence_drv.fallback_timer);
275 276

	amdgpu_fence_process(ring);
A
Alex Deucher 已提交
277 278 279 280 281 282 283 284 285 286 287 288 289
}

/**
 * amdgpu_fence_wait_empty - wait for all fences to signal
 *
 * @adev: amdgpu device pointer
 * @ring: ring index the fence is associated with
 *
 * Wait for all fences on the requested ring to signal (all asics).
 * Returns 0 if the fences have passed, error for all other cases.
 */
int amdgpu_fence_wait_empty(struct amdgpu_ring *ring)
{
290
	uint64_t seq = READ_ONCE(ring->fence_drv.sync_seq);
291
	struct dma_fence *fence, **ptr;
292
	int r;
293

294
	if (!seq)
A
Alex Deucher 已提交
295 296
		return 0;

297 298 299
	ptr = &ring->fence_drv.fences[seq & ring->fence_drv.num_fences_mask];
	rcu_read_lock();
	fence = rcu_dereference(*ptr);
300
	if (!fence || !dma_fence_get_rcu(fence)) {
301 302 303 304 305
		rcu_read_unlock();
		return 0;
	}
	rcu_read_unlock();

306 307
	r = dma_fence_wait(fence, false);
	dma_fence_put(fence);
308
	return r;
A
Alex Deucher 已提交
309 310
}

311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
/**
 * amdgpu_fence_wait_polling - busy wait for givn sequence number
 *
 * @ring: ring index the fence is associated with
 * @wait_seq: sequence number to wait
 * @timeout: the timeout for waiting in usecs
 *
 * Wait for all fences on the requested ring to signal (all asics).
 * Returns left time if no timeout, 0 or minus if timeout.
 */
signed long amdgpu_fence_wait_polling(struct amdgpu_ring *ring,
				      uint32_t wait_seq,
				      signed long timeout)
{
	uint32_t seq;

	do {
		seq = amdgpu_fence_read(ring);
		udelay(5);
		timeout -= 5;
	} while ((int32_t)(wait_seq - seq) > 0 && timeout > 0);

	return timeout > 0 ? timeout : 0;
}
A
Alex Deucher 已提交
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
/**
 * amdgpu_fence_count_emitted - get the count of emitted fences
 *
 * @ring: ring the fence is associated with
 *
 * Get the number of fences emitted on the requested ring (all asics).
 * Returns the number of emitted fences on the ring.  Used by the
 * dynpm code to ring track activity.
 */
unsigned amdgpu_fence_count_emitted(struct amdgpu_ring *ring)
{
	uint64_t emitted;

	/* We are not protected by ring lock when reading the last sequence
	 * but it's ok to report slightly wrong fence count here.
	 */
	amdgpu_fence_process(ring);
352 353
	emitted = 0x100000000ull;
	emitted -= atomic_read(&ring->fence_drv.last_seq);
354
	emitted += READ_ONCE(ring->fence_drv.sync_seq);
355
	return lower_32_bits(emitted);
A
Alex Deucher 已提交
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
}

/**
 * amdgpu_fence_driver_start_ring - make the fence driver
 * ready for use on the requested ring.
 *
 * @ring: ring to start the fence driver on
 * @irq_src: interrupt source to use for this ring
 * @irq_type: interrupt type to use for this ring
 *
 * Make the fence driver ready for processing (all asics).
 * Not all asics have all rings, so each asic will only
 * start the fence driver on the rings it has.
 * Returns 0 for success, errors for failure.
 */
int amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring,
				   struct amdgpu_irq_src *irq_src,
				   unsigned irq_type)
{
	struct amdgpu_device *adev = ring->adev;
	uint64_t index;

	if (ring != &adev->uvd.ring) {
		ring->fence_drv.cpu_addr = &adev->wb.wb[ring->fence_offs];
		ring->fence_drv.gpu_addr = adev->wb.gpu_addr + (ring->fence_offs * 4);
	} else {
		/* put fence directly behind firmware */
		index = ALIGN(adev->uvd.fw->size, 8);
		ring->fence_drv.cpu_addr = adev->uvd.cpu_addr + index;
		ring->fence_drv.gpu_addr = adev->uvd.gpu_addr + index;
	}
387
	amdgpu_fence_write(ring, atomic_read(&ring->fence_drv.last_seq));
388 389
	amdgpu_irq_get(adev, irq_src, irq_type);

A
Alex Deucher 已提交
390 391
	ring->fence_drv.irq_src = irq_src;
	ring->fence_drv.irq_type = irq_type;
392 393
	ring->fence_drv.initialized = true;

394 395 396
	dev_dbg(adev->dev, "fence driver on ring %d use gpu addr 0x%016llx, "
		"cpu addr 0x%p\n", ring->idx,
		ring->fence_drv.gpu_addr, ring->fence_drv.cpu_addr);
A
Alex Deucher 已提交
397 398 399 400 401 402 403 404
	return 0;
}

/**
 * amdgpu_fence_driver_init_ring - init the fence driver
 * for the requested ring.
 *
 * @ring: ring to init the fence driver on
405
 * @num_hw_submission: number of entries on the hardware queue
A
Alex Deucher 已提交
406 407 408 409
 *
 * Init the fence driver for the requested ring (all asics).
 * Helper function for amdgpu_fence_driver_init().
 */
410 411
int amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring,
				  unsigned num_hw_submission)
A
Alex Deucher 已提交
412
{
413
	long timeout;
414
	int r;
A
Alex Deucher 已提交
415

416 417 418 419
	/* Check that num_hw_submission is a power of two */
	if ((num_hw_submission & (num_hw_submission - 1)) != 0)
		return -EINVAL;

A
Alex Deucher 已提交
420 421
	ring->fence_drv.cpu_addr = NULL;
	ring->fence_drv.gpu_addr = 0;
422
	ring->fence_drv.sync_seq = 0;
423
	atomic_set(&ring->fence_drv.last_seq, 0);
A
Alex Deucher 已提交
424 425
	ring->fence_drv.initialized = false;

426
	timer_setup(&ring->fence_drv.fallback_timer, amdgpu_fence_fallback, 0);
427

C
Chunming Zhou 已提交
428
	ring->fence_drv.num_fences_mask = num_hw_submission * 2 - 1;
429
	spin_lock_init(&ring->fence_drv.lock);
C
Chunming Zhou 已提交
430
	ring->fence_drv.fences = kcalloc(num_hw_submission * 2, sizeof(void *),
431 432 433
					 GFP_KERNEL);
	if (!ring->fence_drv.fences)
		return -ENOMEM;
434

435 436 437 438 439 440 441 442 443 444 445 446 447
	/* No need to setup the GPU scheduler for KIQ ring */
	if (ring->funcs->type != AMDGPU_RING_TYPE_KIQ) {
		timeout = msecs_to_jiffies(amdgpu_lockup_timeout);
		if (timeout == 0) {
			/*
			 * FIXME:
			 * Delayed workqueue cannot use it directly,
			 * so the scheduler will not use delayed workqueue if
			 * MAX_SCHEDULE_TIMEOUT is set.
			 * Currently keep it simple and silly.
			 */
			timeout = MAX_SCHEDULE_TIMEOUT;
		}
448
		r = drm_sched_init(&ring->sched, &amdgpu_sched_ops,
M
Monk Liu 已提交
449
				   num_hw_submission, amdgpu_job_hang_limit,
450 451 452 453 454 455
				   timeout, ring->name);
		if (r) {
			DRM_ERROR("Failed to create scheduler on ring %s.\n",
				  ring->name);
			return r;
		}
456
	}
457 458

	return 0;
A
Alex Deucher 已提交
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
}

/**
 * amdgpu_fence_driver_init - init the fence driver
 * for all possible rings.
 *
 * @adev: amdgpu device pointer
 *
 * Init the fence driver for all possible rings (all asics).
 * Not all asics have all rings, so each asic will only
 * start the fence driver on the rings it has using
 * amdgpu_fence_driver_start_ring().
 * Returns 0 for success.
 */
int amdgpu_fence_driver_init(struct amdgpu_device *adev)
{
	if (amdgpu_debugfs_fence_init(adev))
		dev_err(adev->dev, "fence debugfs file creation failed\n");

	return 0;
}

/**
 * amdgpu_fence_driver_fini - tear down the fence driver
 * for all possible rings.
 *
 * @adev: amdgpu device pointer
 *
 * Tear down the fence driver for all possible rings (all asics).
 */
void amdgpu_fence_driver_fini(struct amdgpu_device *adev)
{
491 492
	unsigned i, j;
	int r;
A
Alex Deucher 已提交
493 494 495

	for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
		struct amdgpu_ring *ring = adev->rings[i];
496

A
Alex Deucher 已提交
497 498 499 500 501
		if (!ring || !ring->fence_drv.initialized)
			continue;
		r = amdgpu_fence_wait_empty(ring);
		if (r) {
			/* no need to trigger GPU reset as we are unloading */
M
Monk Liu 已提交
502
			amdgpu_fence_driver_force_completion(ring);
A
Alex Deucher 已提交
503
		}
504 505
		amdgpu_irq_put(adev, ring->fence_drv.irq_src,
			       ring->fence_drv.irq_type);
506
		drm_sched_fini(&ring->sched);
507
		del_timer_sync(&ring->fence_drv.fallback_timer);
508
		for (j = 0; j <= ring->fence_drv.num_fences_mask; ++j)
509
			dma_fence_put(ring->fence_drv.fences[j]);
510
		kfree(ring->fence_drv.fences);
511
		ring->fence_drv.fences = NULL;
A
Alex Deucher 已提交
512 513 514 515
		ring->fence_drv.initialized = false;
	}
}

516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
/**
 * amdgpu_fence_driver_suspend - suspend the fence driver
 * for all possible rings.
 *
 * @adev: amdgpu device pointer
 *
 * Suspend the fence driver for all possible rings (all asics).
 */
void amdgpu_fence_driver_suspend(struct amdgpu_device *adev)
{
	int i, r;

	for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
		struct amdgpu_ring *ring = adev->rings[i];
		if (!ring || !ring->fence_drv.initialized)
			continue;

		/* wait for gpu to finish processing current batch */
		r = amdgpu_fence_wait_empty(ring);
		if (r) {
			/* delay GPU reset to resume */
M
Monk Liu 已提交
537
			amdgpu_fence_driver_force_completion(ring);
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
		}

		/* disable the interrupt */
		amdgpu_irq_put(adev, ring->fence_drv.irq_src,
			       ring->fence_drv.irq_type);
	}
}

/**
 * amdgpu_fence_driver_resume - resume the fence driver
 * for all possible rings.
 *
 * @adev: amdgpu device pointer
 *
 * Resume the fence driver for all possible rings (all asics).
 * Not all asics have all rings, so each asic will only
 * start the fence driver on the rings it has using
 * amdgpu_fence_driver_start_ring().
 * Returns 0 for success.
 */
void amdgpu_fence_driver_resume(struct amdgpu_device *adev)
{
	int i;

	for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
		struct amdgpu_ring *ring = adev->rings[i];
		if (!ring || !ring->fence_drv.initialized)
			continue;

		/* enable the interrupt */
		amdgpu_irq_get(adev, ring->fence_drv.irq_src,
			       ring->fence_drv.irq_type);
	}
}

A
Alex Deucher 已提交
573
/**
M
Monk Liu 已提交
574
 * amdgpu_fence_driver_force_completion - force signal latest fence of ring
A
Alex Deucher 已提交
575
 *
M
Monk Liu 已提交
576
 * @ring: fence of the ring to signal
A
Alex Deucher 已提交
577 578
 *
 */
M
Monk Liu 已提交
579
void amdgpu_fence_driver_force_completion(struct amdgpu_ring *ring)
A
Alex Deucher 已提交
580
{
M
Monk Liu 已提交
581 582
	amdgpu_fence_write(ring, ring->fence_drv.sync_seq);
	amdgpu_fence_process(ring);
583 584
}

585 586 587 588
/*
 * Common fence implementation
 */

589
static const char *amdgpu_fence_get_driver_name(struct dma_fence *fence)
590 591 592 593
{
	return "amdgpu";
}

594
static const char *amdgpu_fence_get_timeline_name(struct dma_fence *f)
595 596 597 598 599 600 601 602 603 604 605 606 607
{
	struct amdgpu_fence *fence = to_amdgpu_fence(f);
	return (const char *)fence->ring->name;
}

/**
 * amdgpu_fence_enable_signaling - enable signalling on fence
 * @fence: fence
 *
 * This function is called with fence_queue lock held, and adds a callback
 * to fence_queue that checks if this fence is signaled, and if so it
 * signals the fence and removes itself.
 */
608
static bool amdgpu_fence_enable_signaling(struct dma_fence *f)
609 610 611 612
{
	struct amdgpu_fence *fence = to_amdgpu_fence(f);
	struct amdgpu_ring *ring = fence->ring;

613 614
	if (!timer_pending(&ring->fence_drv.fallback_timer))
		amdgpu_fence_schedule_fallback(ring);
615

616
	DMA_FENCE_TRACE(&fence->base, "armed on ring %i!\n", ring->idx);
617

618 619 620
	return true;
}

621 622 623 624 625 626 627 628
/**
 * amdgpu_fence_free - free up the fence memory
 *
 * @rcu: RCU callback head
 *
 * Free up the fence memory after the RCU grace period.
 */
static void amdgpu_fence_free(struct rcu_head *rcu)
629
{
630
	struct dma_fence *f = container_of(rcu, struct dma_fence, rcu);
631 632 633 634
	struct amdgpu_fence *fence = to_amdgpu_fence(f);
	kmem_cache_free(amdgpu_fence_slab, fence);
}

635 636 637 638 639 640 641 642
/**
 * amdgpu_fence_release - callback that fence can be freed
 *
 * @fence: fence
 *
 * This function is called when the reference count becomes zero.
 * It just RCU schedules freeing up the fence.
 */
643
static void amdgpu_fence_release(struct dma_fence *f)
644 645 646 647
{
	call_rcu(&f->rcu, amdgpu_fence_free);
}

648
static const struct dma_fence_ops amdgpu_fence_ops = {
649 650 651
	.get_driver_name = amdgpu_fence_get_driver_name,
	.get_timeline_name = amdgpu_fence_get_timeline_name,
	.enable_signaling = amdgpu_fence_enable_signaling,
652
	.wait = dma_fence_default_wait,
653
	.release = amdgpu_fence_release,
654
};
A
Alex Deucher 已提交
655 656 657 658 659 660 661 662 663 664

/*
 * Fence debugfs
 */
#if defined(CONFIG_DEBUG_FS)
static int amdgpu_debugfs_fence_info(struct seq_file *m, void *data)
{
	struct drm_info_node *node = (struct drm_info_node *)m->private;
	struct drm_device *dev = node->minor->dev;
	struct amdgpu_device *adev = dev->dev_private;
665
	int i;
A
Alex Deucher 已提交
666 667 668 669 670 671 672 673

	for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
		struct amdgpu_ring *ring = adev->rings[i];
		if (!ring || !ring->fence_drv.initialized)
			continue;

		amdgpu_fence_process(ring);

674
		seq_printf(m, "--- ring %d (%s) ---\n", i, ring->name);
675 676 677
		seq_printf(m, "Last signaled fence 0x%08x\n",
			   atomic_read(&ring->fence_drv.last_seq));
		seq_printf(m, "Last emitted        0x%08x\n",
678
			   ring->fence_drv.sync_seq);
P
pding 已提交
679 680 681 682 683 684 685 686 687 688 689 690 691

		if (ring->funcs->type != AMDGPU_RING_TYPE_GFX)
			continue;

		/* set in CP_VMID_PREEMPT and preemption occurred */
		seq_printf(m, "Last preempted      0x%08x\n",
			   le32_to_cpu(*(ring->fence_drv.cpu_addr + 2)));
		/* set in CP_VMID_RESET and reset occurred */
		seq_printf(m, "Last reset          0x%08x\n",
			   le32_to_cpu(*(ring->fence_drv.cpu_addr + 4)));
		/* Both preemption and reset occurred */
		seq_printf(m, "Last both           0x%08x\n",
			   le32_to_cpu(*(ring->fence_drv.cpu_addr + 6)));
A
Alex Deucher 已提交
692 693 694 695
	}
	return 0;
}

696
/**
697
 * amdgpu_debugfs_gpu_recover - manually trigger a gpu reset & recover
698 699 700
 *
 * Manually trigger a gpu reset at the next fence wait.
 */
701
static int amdgpu_debugfs_gpu_recover(struct seq_file *m, void *data)
702 703 704 705 706
{
	struct drm_info_node *node = (struct drm_info_node *) m->private;
	struct drm_device *dev = node->minor->dev;
	struct amdgpu_device *adev = dev->dev_private;

707 708
	seq_printf(m, "gpu recover\n");
	amdgpu_gpu_recover(adev, NULL);
709 710 711 712

	return 0;
}

713
static const struct drm_info_list amdgpu_debugfs_fence_list[] = {
A
Alex Deucher 已提交
714
	{"amdgpu_fence_info", &amdgpu_debugfs_fence_info, 0, NULL},
715
	{"amdgpu_gpu_recover", &amdgpu_debugfs_gpu_recover, 0, NULL}
A
Alex Deucher 已提交
716
};
717 718 719 720

static const struct drm_info_list amdgpu_debugfs_fence_list_sriov[] = {
	{"amdgpu_fence_info", &amdgpu_debugfs_fence_info, 0, NULL},
};
A
Alex Deucher 已提交
721 722 723 724 725
#endif

int amdgpu_debugfs_fence_init(struct amdgpu_device *adev)
{
#if defined(CONFIG_DEBUG_FS)
726 727
	if (amdgpu_sriov_vf(adev))
		return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_fence_list_sriov, 1);
728
	return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_fence_list, 2);
A
Alex Deucher 已提交
729 730 731 732 733
#else
	return 0;
#endif
}