xref: /openbsd-src/sys/dev/pci/drm/radeon/radeon_ring.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: radeon_ring.c,v 1.8 2015/07/11 04:00:46 jsg Exp $	*/
2 /*
3  * Copyright 2008 Advanced Micro Devices, Inc.
4  * Copyright 2008 Red Hat Inc.
5  * Copyright 2009 Jerome Glisse.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation
10  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11  * and/or sell copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
21  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23  * OTHER DEALINGS IN THE SOFTWARE.
24  *
25  * Authors: Dave Airlie
26  *          Alex Deucher
27  *          Jerome Glisse
28  *          Christian König
29  */
30 #include <dev/pci/drm/drmP.h>
31 #include <dev/pci/drm/radeon_drm.h>
32 #include "radeon_reg.h"
33 #include "radeon.h"
34 #include "atom.h"
35 
36 /*
37  * IB
38  * IBs (Indirect Buffers) and areas of GPU accessible memory where
39  * commands are stored.  You can put a pointer to the IB in the
40  * command ring and the hw will fetch the commands from the IB
41  * and execute them.  Generally userspace acceleration drivers
42  * produce command buffers which are send to the kernel and
43  * put in IBs for execution by the requested ring.
44  */
45 static int radeon_debugfs_sa_init(struct radeon_device *rdev);
46 
47 /**
48  * radeon_ib_get - request an IB (Indirect Buffer)
49  *
50  * @rdev: radeon_device pointer
51  * @ring: ring index the IB is associated with
52  * @ib: IB object returned
53  * @size: requested IB size
54  *
55  * Request an IB (all asics).  IBs are allocated using the
56  * suballocator.
57  * Returns 0 on success, error on failure.
58  */
59 int radeon_ib_get(struct radeon_device *rdev, int ring,
60 		  struct radeon_ib *ib, struct radeon_vm *vm,
61 		  unsigned size)
62 {
63 	int i, r;
64 
65 	r = radeon_sa_bo_new(rdev, &rdev->ring_tmp_bo, &ib->sa_bo, size, 256, true);
66 	if (r) {
67 		dev_err(rdev->dev, "failed to get a new IB (%d)\n", r);
68 		return r;
69 	}
70 
71 	r = radeon_semaphore_create(rdev, &ib->semaphore);
72 	if (r) {
73 		return r;
74 	}
75 
76 	ib->ring = ring;
77 	ib->fence = NULL;
78 	ib->ptr = radeon_sa_bo_cpu_addr(ib->sa_bo);
79 	ib->vm = vm;
80 	if (vm) {
81 		/* ib pool is bound at RADEON_VA_IB_OFFSET in virtual address
82 		 * space and soffset is the offset inside the pool bo
83 		 */
84 		ib->gpu_addr = ib->sa_bo->soffset + RADEON_VA_IB_OFFSET;
85 	} else {
86 		ib->gpu_addr = radeon_sa_bo_gpu_addr(ib->sa_bo);
87 	}
88 	ib->is_const_ib = false;
89 	for (i = 0; i < RADEON_NUM_RINGS; ++i)
90 		ib->sync_to[i] = NULL;
91 
92 	return 0;
93 }
94 
95 /**
96  * radeon_ib_free - free an IB (Indirect Buffer)
97  *
98  * @rdev: radeon_device pointer
99  * @ib: IB object to free
100  *
101  * Free an IB (all asics).
102  */
103 void radeon_ib_free(struct radeon_device *rdev, struct radeon_ib *ib)
104 {
105 	radeon_semaphore_free(rdev, &ib->semaphore, ib->fence);
106 	radeon_sa_bo_free(rdev, &ib->sa_bo, ib->fence);
107 	radeon_fence_unref(&ib->fence);
108 }
109 
110 /**
111  * radeon_ib_schedule - schedule an IB (Indirect Buffer) on the ring
112  *
113  * @rdev: radeon_device pointer
114  * @ib: IB object to schedule
115  * @const_ib: Const IB to schedule (SI only)
116  *
117  * Schedule an IB on the associated ring (all asics).
118  * Returns 0 on success, error on failure.
119  *
120  * On SI, there are two parallel engines fed from the primary ring,
121  * the CE (Constant Engine) and the DE (Drawing Engine).  Since
122  * resource descriptors have moved to memory, the CE allows you to
123  * prime the caches while the DE is updating register state so that
124  * the resource descriptors will be already in cache when the draw is
125  * processed.  To accomplish this, the userspace driver submits two
126  * IBs, one for the CE and one for the DE.  If there is a CE IB (called
127  * a CONST_IB), it will be put on the ring prior to the DE IB.  Prior
128  * to SI there was just a DE IB.
129  */
130 int radeon_ib_schedule(struct radeon_device *rdev, struct radeon_ib *ib,
131 		       struct radeon_ib *const_ib)
132 {
133 	struct radeon_ring *ring = &rdev->ring[ib->ring];
134 	bool need_sync = false;
135 	int i, r = 0;
136 
137 	if (!ib->length_dw || !ring->ready) {
138 		/* TODO: Nothings in the ib we should report. */
139 		dev_err(rdev->dev, "couldn't schedule ib\n");
140 		return -EINVAL;
141 	}
142 
143 	/* 64 dwords should be enough for fence too */
144 	r = radeon_ring_lock(rdev, ring, 64 + RADEON_NUM_RINGS * 8);
145 	if (r) {
146 		dev_err(rdev->dev, "scheduling IB failed (%d).\n", r);
147 		return r;
148 	}
149 	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
150 		struct radeon_fence *fence = ib->sync_to[i];
151 		if (radeon_fence_need_sync(fence, ib->ring)) {
152 			need_sync = true;
153 			radeon_semaphore_sync_rings(rdev, ib->semaphore,
154 						    fence->ring, ib->ring);
155 			radeon_fence_note_sync(fence, ib->ring);
156 		}
157 	}
158 	/* immediately free semaphore when we don't need to sync */
159 	if (!need_sync) {
160 		radeon_semaphore_free(rdev, &ib->semaphore, NULL);
161 	}
162 	/* if we can't remember our last VM flush then flush now! */
163 	/* XXX figure out why we have to flush for every IB */
164 	if (ib->vm /*&& !ib->vm->last_flush*/) {
165 		radeon_ring_vm_flush(rdev, ib->ring, ib->vm);
166 	}
167 	if (const_ib) {
168 		radeon_ring_ib_execute(rdev, const_ib->ring, const_ib);
169 		radeon_semaphore_free(rdev, &const_ib->semaphore, NULL);
170 	}
171 	radeon_ring_ib_execute(rdev, ib->ring, ib);
172 	r = radeon_fence_emit(rdev, &ib->fence, ib->ring);
173 	if (r) {
174 		dev_err(rdev->dev, "failed to emit fence for new IB (%d)\n", r);
175 		radeon_ring_unlock_undo(rdev, ring);
176 		return r;
177 	}
178 	if (const_ib) {
179 		const_ib->fence = radeon_fence_ref(ib->fence);
180 	}
181 	/* we just flushed the VM, remember that */
182 	if (ib->vm && !ib->vm->last_flush) {
183 		ib->vm->last_flush = radeon_fence_ref(ib->fence);
184 	}
185 	radeon_ring_unlock_commit(rdev, ring);
186 	return 0;
187 }
188 
189 /**
190  * radeon_ib_pool_init - Init the IB (Indirect Buffer) pool
191  *
192  * @rdev: radeon_device pointer
193  *
194  * Initialize the suballocator to manage a pool of memory
195  * for use as IBs (all asics).
196  * Returns 0 on success, error on failure.
197  */
198 int radeon_ib_pool_init(struct radeon_device *rdev)
199 {
200 	int r;
201 
202 	if (rdev->ib_pool_ready) {
203 		return 0;
204 	}
205 	r = radeon_sa_bo_manager_init(rdev, &rdev->ring_tmp_bo,
206 				      RADEON_IB_POOL_SIZE*64*1024,
207 				      RADEON_GPU_PAGE_SIZE,
208 				      RADEON_GEM_DOMAIN_GTT);
209 	if (r) {
210 		return r;
211 	}
212 
213 	r = radeon_sa_bo_manager_start(rdev, &rdev->ring_tmp_bo);
214 	if (r) {
215 		return r;
216 	}
217 
218 	rdev->ib_pool_ready = true;
219 	if (radeon_debugfs_sa_init(rdev)) {
220 		dev_err(rdev->dev, "failed to register debugfs file for SA\n");
221 	}
222 	return 0;
223 }
224 
225 /**
226  * radeon_ib_pool_fini - Free the IB (Indirect Buffer) pool
227  *
228  * @rdev: radeon_device pointer
229  *
230  * Tear down the suballocator managing the pool of memory
231  * for use as IBs (all asics).
232  */
233 void radeon_ib_pool_fini(struct radeon_device *rdev)
234 {
235 	if (rdev->ib_pool_ready) {
236 		radeon_sa_bo_manager_suspend(rdev, &rdev->ring_tmp_bo);
237 		radeon_sa_bo_manager_fini(rdev, &rdev->ring_tmp_bo);
238 		rdev->ib_pool_ready = false;
239 	}
240 }
241 
242 /**
243  * radeon_ib_ring_tests - test IBs on the rings
244  *
245  * @rdev: radeon_device pointer
246  *
247  * Test an IB (Indirect Buffer) on each ring.
248  * If the test fails, disable the ring.
249  * Returns 0 on success, error if the primary GFX ring
250  * IB test fails.
251  */
252 int radeon_ib_ring_tests(struct radeon_device *rdev)
253 {
254 	unsigned i;
255 	int r;
256 
257 	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
258 		struct radeon_ring *ring = &rdev->ring[i];
259 
260 		if (!ring->ready)
261 			continue;
262 
263 		r = radeon_ib_test(rdev, i, ring);
264 		if (r) {
265 			ring->ready = false;
266 
267 			if (i == RADEON_RING_TYPE_GFX_INDEX) {
268 				/* oh, oh, that's really bad */
269 				DRM_ERROR("radeon: failed testing IB on GFX ring (%d).\n", r);
270 		                rdev->accel_working = false;
271 				return r;
272 
273 			} else {
274 				/* still not good, but we can live with it */
275 				DRM_ERROR("radeon: failed testing IB on ring %d (%d).\n", i, r);
276 			}
277 		}
278 	}
279 	return 0;
280 }
281 
282 /*
283  * Rings
284  * Most engines on the GPU are fed via ring buffers.  Ring
285  * buffers are areas of GPU accessible memory that the host
286  * writes commands into and the GPU reads commands out of.
287  * There is a rptr (read pointer) that determines where the
288  * GPU is currently reading, and a wptr (write pointer)
289  * which determines where the host has written.  When the
290  * pointers are equal, the ring is idle.  When the host
291  * writes commands to the ring buffer, it increments the
292  * wptr.  The GPU then starts fetching commands and executes
293  * them until the pointers are equal again.
294  */
295 static int radeon_debugfs_ring_init(struct radeon_device *rdev, struct radeon_ring *ring);
296 
297 #if defined(DRM_DEBUG_CODE) && DRM_DEBUG_CODE != 0
298 /**
299  * radeon_ring_write - write a value to the ring
300  *
301  * @ring: radeon_ring structure holding ring information
302  * @v: dword (dw) value to write
303  *
304  * Write a value to the requested ring buffer (all asics).
305  */
306 void radeon_ring_write(struct radeon_ring *ring, uint32_t v)
307 {
308 #if DRM_DEBUG_CODE
309 	if (ring->count_dw <= 0) {
310 		DRM_ERROR("radeon: writing more dwords to the ring than expected!\n");
311 	}
312 #endif
313 	ring->ring[ring->wptr++] = v;
314 	ring->wptr &= ring->ptr_mask;
315 	ring->count_dw--;
316 	ring->ring_free_dw--;
317 }
318 #endif
319 
320 /**
321  * radeon_ring_supports_scratch_reg - check if the ring supports
322  * writing to scratch registers
323  *
324  * @rdev: radeon_device pointer
325  * @ring: radeon_ring structure holding ring information
326  *
327  * Check if a specific ring supports writing to scratch registers (all asics).
328  * Returns true if the ring supports writing to scratch regs, false if not.
329  */
330 bool radeon_ring_supports_scratch_reg(struct radeon_device *rdev,
331 				      struct radeon_ring *ring)
332 {
333 	switch (ring->idx) {
334 	case RADEON_RING_TYPE_GFX_INDEX:
335 	case CAYMAN_RING_TYPE_CP1_INDEX:
336 	case CAYMAN_RING_TYPE_CP2_INDEX:
337 		return true;
338 	default:
339 		return false;
340 	}
341 }
342 
343 /**
344  * radeon_ring_free_size - update the free size
345  *
346  * @rdev: radeon_device pointer
347  * @ring: radeon_ring structure holding ring information
348  *
349  * Update the free dw slots in the ring buffer (all asics).
350  */
351 void radeon_ring_free_size(struct radeon_device *rdev, struct radeon_ring *ring)
352 {
353 	u32 rptr;
354 
355 	if (rdev->wb.enabled)
356 		rptr = le32_to_cpu(rdev->wb.wb[ring->rptr_offs/4]);
357 	else
358 		rptr = RREG32(ring->rptr_reg);
359 	ring->rptr = (rptr & ring->ptr_reg_mask) >> ring->ptr_reg_shift;
360 	/* This works because ring_size is a power of 2 */
361 	ring->ring_free_dw = (ring->rptr + (ring->ring_size / 4));
362 	ring->ring_free_dw -= ring->wptr;
363 	ring->ring_free_dw &= ring->ptr_mask;
364 	if (!ring->ring_free_dw) {
365 		ring->ring_free_dw = ring->ring_size / 4;
366 	}
367 }
368 
369 /**
370  * radeon_ring_alloc - allocate space on the ring buffer
371  *
372  * @rdev: radeon_device pointer
373  * @ring: radeon_ring structure holding ring information
374  * @ndw: number of dwords to allocate in the ring buffer
375  *
376  * Allocate @ndw dwords in the ring buffer (all asics).
377  * Returns 0 on success, error on failure.
378  */
379 int radeon_ring_alloc(struct radeon_device *rdev, struct radeon_ring *ring, unsigned ndw)
380 {
381 	int r;
382 
383 	/* make sure we aren't trying to allocate more space than there is on the ring */
384 	if (ndw > (ring->ring_size / 4))
385 		return -ENOMEM;
386 	/* Align requested size with padding so unlock_commit can
387 	 * pad safely */
388 	radeon_ring_free_size(rdev, ring);
389 	if (ring->ring_free_dw == (ring->ring_size / 4)) {
390 		/* This is an empty ring update lockup info to avoid
391 		 * false positive.
392 		 */
393 		radeon_ring_lockup_update(ring);
394 	}
395 	ndw = (ndw + ring->align_mask) & ~ring->align_mask;
396 	while (ndw > (ring->ring_free_dw - 1)) {
397 		radeon_ring_free_size(rdev, ring);
398 		if (ndw < ring->ring_free_dw) {
399 			break;
400 		}
401 		r = radeon_fence_wait_next_locked(rdev, ring->idx);
402 		if (r)
403 			return r;
404 	}
405 	ring->count_dw = ndw;
406 	ring->wptr_old = ring->wptr;
407 	return 0;
408 }
409 
410 /**
411  * radeon_ring_lock - lock the ring and allocate space on it
412  *
413  * @rdev: radeon_device pointer
414  * @ring: radeon_ring structure holding ring information
415  * @ndw: number of dwords to allocate in the ring buffer
416  *
417  * Lock the ring and allocate @ndw dwords in the ring buffer
418  * (all asics).
419  * Returns 0 on success, error on failure.
420  */
421 int radeon_ring_lock(struct radeon_device *rdev, struct radeon_ring *ring, unsigned ndw)
422 {
423 	int r;
424 
425 	mutex_lock(&rdev->ring_lock);
426 	r = radeon_ring_alloc(rdev, ring, ndw);
427 	if (r) {
428 		mutex_unlock(&rdev->ring_lock);
429 		return r;
430 	}
431 	return 0;
432 }
433 
434 /**
435  * radeon_ring_commit - tell the GPU to execute the new
436  * commands on the ring buffer
437  *
438  * @rdev: radeon_device pointer
439  * @ring: radeon_ring structure holding ring information
440  *
441  * Update the wptr (write pointer) to tell the GPU to
442  * execute new commands on the ring buffer (all asics).
443  */
444 void radeon_ring_commit(struct radeon_device *rdev, struct radeon_ring *ring)
445 {
446 	/* We pad to match fetch size */
447 	while (ring->wptr & ring->align_mask) {
448 		radeon_ring_write(ring, ring->nop);
449 	}
450 	DRM_MEMORYBARRIER();
451 	WREG32(ring->wptr_reg, (ring->wptr << ring->ptr_reg_shift) & ring->ptr_reg_mask);
452 	(void)RREG32(ring->wptr_reg);
453 }
454 
455 /**
456  * radeon_ring_unlock_commit - tell the GPU to execute the new
457  * commands on the ring buffer and unlock it
458  *
459  * @rdev: radeon_device pointer
460  * @ring: radeon_ring structure holding ring information
461  *
462  * Call radeon_ring_commit() then unlock the ring (all asics).
463  */
464 void radeon_ring_unlock_commit(struct radeon_device *rdev, struct radeon_ring *ring)
465 {
466 	radeon_ring_commit(rdev, ring);
467 	mutex_unlock(&rdev->ring_lock);
468 }
469 
470 /**
471  * radeon_ring_undo - reset the wptr
472  *
473  * @ring: radeon_ring structure holding ring information
474  *
475  * Reset the driver's copy of the wptr (all asics).
476  */
477 void radeon_ring_undo(struct radeon_ring *ring)
478 {
479 	ring->wptr = ring->wptr_old;
480 }
481 
482 /**
483  * radeon_ring_unlock_undo - reset the wptr and unlock the ring
484  *
485  * @ring: radeon_ring structure holding ring information
486  *
487  * Call radeon_ring_undo() then unlock the ring (all asics).
488  */
489 void radeon_ring_unlock_undo(struct radeon_device *rdev, struct radeon_ring *ring)
490 {
491 	radeon_ring_undo(ring);
492 	mutex_unlock(&rdev->ring_lock);
493 }
494 
495 /**
496  * radeon_ring_force_activity - add some nop packets to the ring
497  *
498  * @rdev: radeon_device pointer
499  * @ring: radeon_ring structure holding ring information
500  *
501  * Add some nop packets to the ring to force activity (all asics).
502  * Used for lockup detection to see if the rptr is advancing.
503  */
504 void radeon_ring_force_activity(struct radeon_device *rdev, struct radeon_ring *ring)
505 {
506 	int r;
507 
508 	radeon_ring_free_size(rdev, ring);
509 	if (ring->rptr == ring->wptr) {
510 		r = radeon_ring_alloc(rdev, ring, 1);
511 		if (!r) {
512 			radeon_ring_write(ring, ring->nop);
513 			radeon_ring_commit(rdev, ring);
514 		}
515 	}
516 }
517 
518 /**
519  * radeon_ring_lockup_update - update lockup variables
520  *
521  * @ring: radeon_ring structure holding ring information
522  *
523  * Update the last rptr value and timestamp (all asics).
524  */
525 void radeon_ring_lockup_update(struct radeon_ring *ring)
526 {
527 	ring->last_rptr = ring->rptr;
528 	ring->last_activity = jiffies;
529 }
530 
531 /**
532  * radeon_ring_test_lockup() - check if ring is lockedup by recording information
533  * @rdev:       radeon device structure
534  * @ring:       radeon_ring structure holding ring information
535  *
536  * We don't need to initialize the lockup tracking information as we will either
537  * have CP rptr to a different value of jiffies wrap around which will force
538  * initialization of the lockup tracking informations.
539  *
540  * A possible false positivie is if we get call after while and last_cp_rptr ==
541  * the current CP rptr, even if it's unlikely it might happen. To avoid this
542  * if the elapsed time since last call is bigger than 2 second than we return
543  * false and update the tracking information. Due to this the caller must call
544  * radeon_ring_test_lockup several time in less than 2sec for lockup to be reported
545  * the fencing code should be cautious about that.
546  *
547  * Caller should write to the ring to force CP to do something so we don't get
548  * false positive when CP is just gived nothing to do.
549  *
550  **/
551 bool radeon_ring_test_lockup(struct radeon_device *rdev, struct radeon_ring *ring)
552 {
553 	unsigned long cjiffies, elapsed;
554 	uint32_t rptr;
555 
556 	cjiffies = jiffies;
557 	if (!time_after(cjiffies, ring->last_activity)) {
558 		/* likely a wrap around */
559 		radeon_ring_lockup_update(ring);
560 		return false;
561 	}
562 	rptr = RREG32(ring->rptr_reg);
563 	ring->rptr = (rptr & ring->ptr_reg_mask) >> ring->ptr_reg_shift;
564 	if (ring->rptr != ring->last_rptr) {
565 		/* CP is still working no lockup */
566 		radeon_ring_lockup_update(ring);
567 		return false;
568 	}
569 	elapsed = jiffies_to_msecs(cjiffies - ring->last_activity);
570 	if (radeon_lockup_timeout && elapsed >= radeon_lockup_timeout) {
571 		dev_err(rdev->dev, "GPU lockup CP stall for more than %lumsec\n", elapsed);
572 		return true;
573 	}
574 	/* give a chance to the GPU ... */
575 	return false;
576 }
577 
578 /**
579  * radeon_ring_backup - Back up the content of a ring
580  *
581  * @rdev: radeon_device pointer
582  * @ring: the ring we want to back up
583  *
584  * Saves all unprocessed commits from a ring, returns the number of dwords saved.
585  */
586 unsigned radeon_ring_backup(struct radeon_device *rdev, struct radeon_ring *ring,
587 			    uint32_t **data)
588 {
589 	unsigned size, ptr, i;
590 
591 	/* just in case lock the ring */
592 	mutex_lock(&rdev->ring_lock);
593 	*data = NULL;
594 
595 	if (ring->ring_obj == NULL) {
596 		mutex_unlock(&rdev->ring_lock);
597 		return 0;
598 	}
599 
600 	/* it doesn't make sense to save anything if all fences are signaled */
601 	if (!radeon_fence_count_emitted(rdev, ring->idx)) {
602 		mutex_unlock(&rdev->ring_lock);
603 		return 0;
604 	}
605 
606 	/* calculate the number of dw on the ring */
607 	if (ring->rptr_save_reg)
608 		ptr = RREG32(ring->rptr_save_reg);
609 	else if (rdev->wb.enabled)
610 		ptr = le32_to_cpu(*ring->next_rptr_cpu_addr);
611 	else {
612 		/* no way to read back the next rptr */
613 		mutex_unlock(&rdev->ring_lock);
614 		return 0;
615 	}
616 
617 	size = ring->wptr + (ring->ring_size / 4);
618 	size -= ptr;
619 	size &= ring->ptr_mask;
620 	if (size == 0) {
621 		mutex_unlock(&rdev->ring_lock);
622 		return 0;
623 	}
624 
625 	/* and then save the content of the ring */
626 	*data = kmalloc_array(size, sizeof(uint32_t), GFP_KERNEL);
627 	if (!*data) {
628 		mutex_unlock(&rdev->ring_lock);
629 		return 0;
630 	}
631 	for (i = 0; i < size; ++i) {
632 		(*data)[i] = ring->ring[ptr++];
633 		ptr &= ring->ptr_mask;
634 	}
635 
636 	mutex_unlock(&rdev->ring_lock);
637 	return size;
638 }
639 
640 /**
641  * radeon_ring_restore - append saved commands to the ring again
642  *
643  * @rdev: radeon_device pointer
644  * @ring: ring to append commands to
645  * @size: number of dwords we want to write
646  * @data: saved commands
647  *
648  * Allocates space on the ring and restore the previously saved commands.
649  */
650 int radeon_ring_restore(struct radeon_device *rdev, struct radeon_ring *ring,
651 			unsigned size, uint32_t *data)
652 {
653 	int i, r;
654 
655 	if (!size || !data)
656 		return 0;
657 
658 	/* restore the saved ring content */
659 	r = radeon_ring_lock(rdev, ring, size);
660 	if (r)
661 		return r;
662 
663 	for (i = 0; i < size; ++i) {
664 		radeon_ring_write(ring, data[i]);
665 	}
666 
667 	radeon_ring_unlock_commit(rdev, ring);
668 	kfree(data);
669 	return 0;
670 }
671 
672 /**
673  * radeon_ring_init - init driver ring struct.
674  *
675  * @rdev: radeon_device pointer
676  * @ring: radeon_ring structure holding ring information
677  * @ring_size: size of the ring
678  * @rptr_offs: offset of the rptr writeback location in the WB buffer
679  * @rptr_reg: MMIO offset of the rptr register
680  * @wptr_reg: MMIO offset of the wptr register
681  * @ptr_reg_shift: bit offset of the rptr/wptr values
682  * @ptr_reg_mask: bit mask of the rptr/wptr values
683  * @nop: nop packet for this ring
684  *
685  * Initialize the driver information for the selected ring (all asics).
686  * Returns 0 on success, error on failure.
687  */
688 int radeon_ring_init(struct radeon_device *rdev, struct radeon_ring *ring, unsigned ring_size,
689 		     unsigned rptr_offs, unsigned rptr_reg, unsigned wptr_reg,
690 		     u32 ptr_reg_shift, u32 ptr_reg_mask, u32 nop)
691 {
692 	int r;
693 
694 	ring->ring_size = ring_size;
695 	ring->rptr_offs = rptr_offs;
696 	ring->rptr_reg = rptr_reg;
697 	ring->wptr_reg = wptr_reg;
698 	ring->ptr_reg_shift = ptr_reg_shift;
699 	ring->ptr_reg_mask = ptr_reg_mask;
700 	ring->nop = nop;
701 	/* Allocate ring buffer */
702 	if (ring->ring_obj == NULL) {
703 		r = radeon_bo_create(rdev, ring->ring_size, PAGE_SIZE, true,
704 				     RADEON_GEM_DOMAIN_GTT,
705 				     NULL, &ring->ring_obj);
706 		if (r) {
707 			dev_err(rdev->dev, "(%d) ring create failed\n", r);
708 			return r;
709 		}
710 		r = radeon_bo_reserve(ring->ring_obj, false);
711 		if (unlikely(r != 0))
712 			return r;
713 		r = radeon_bo_pin(ring->ring_obj, RADEON_GEM_DOMAIN_GTT,
714 					&ring->gpu_addr);
715 		if (r) {
716 			radeon_bo_unreserve(ring->ring_obj);
717 			dev_err(rdev->dev, "(%d) ring pin failed\n", r);
718 			return r;
719 		}
720 		r = radeon_bo_kmap(ring->ring_obj,
721 				       (void **)&ring->ring);
722 		radeon_bo_unreserve(ring->ring_obj);
723 		if (r) {
724 			dev_err(rdev->dev, "(%d) ring map failed\n", r);
725 			return r;
726 		}
727 	}
728 	ring->ptr_mask = (ring->ring_size / 4) - 1;
729 	ring->ring_free_dw = ring->ring_size / 4;
730 	if (rdev->wb.enabled) {
731 		u32 index = RADEON_WB_RING0_NEXT_RPTR + (ring->idx * 4);
732 		ring->next_rptr_gpu_addr = rdev->wb.gpu_addr + index;
733 		ring->next_rptr_cpu_addr = &rdev->wb.wb[index/4];
734 	}
735 	if (radeon_debugfs_ring_init(rdev, ring)) {
736 		DRM_ERROR("Failed to register debugfs file for rings !\n");
737 	}
738 	radeon_ring_lockup_update(ring);
739 	return 0;
740 }
741 
742 /**
743  * radeon_ring_fini - tear down the driver ring struct.
744  *
745  * @rdev: radeon_device pointer
746  * @ring: radeon_ring structure holding ring information
747  *
748  * Tear down the driver information for the selected ring (all asics).
749  */
750 void radeon_ring_fini(struct radeon_device *rdev, struct radeon_ring *ring)
751 {
752 	int r;
753 	struct radeon_bo *ring_obj;
754 
755 	mutex_lock(&rdev->ring_lock);
756 	ring_obj = ring->ring_obj;
757 	ring->ready = false;
758 	ring->ring = NULL;
759 	ring->ring_obj = NULL;
760 	mutex_unlock(&rdev->ring_lock);
761 
762 	if (ring_obj) {
763 		r = radeon_bo_reserve(ring_obj, false);
764 		if (likely(r == 0)) {
765 			radeon_bo_kunmap(ring_obj);
766 			radeon_bo_unpin(ring_obj);
767 			radeon_bo_unreserve(ring_obj);
768 		}
769 		radeon_bo_unref(&ring_obj);
770 	}
771 }
772 
773 /*
774  * Debugfs info
775  */
776 #if defined(CONFIG_DEBUG_FS)
777 
778 static int radeon_debugfs_ring_info(struct seq_file *m, void *data)
779 {
780 	struct drm_info_node *node = (struct drm_info_node *) m->private;
781 	struct drm_device *dev = node->minor->dev;
782 	struct radeon_device *rdev = dev->dev_private;
783 	int ridx = *(int*)node->info_ent->data;
784 	struct radeon_ring *ring = &rdev->ring[ridx];
785 	unsigned count, i, j;
786 	u32 tmp;
787 
788 	radeon_ring_free_size(rdev, ring);
789 	count = (ring->ring_size / 4) - ring->ring_free_dw;
790 	tmp = RREG32(ring->wptr_reg) >> ring->ptr_reg_shift;
791 	seq_printf(m, "wptr(0x%04x): 0x%08x [%5d]\n", ring->wptr_reg, tmp, tmp);
792 	tmp = RREG32(ring->rptr_reg) >> ring->ptr_reg_shift;
793 	seq_printf(m, "rptr(0x%04x): 0x%08x [%5d]\n", ring->rptr_reg, tmp, tmp);
794 	if (ring->rptr_save_reg) {
795 		seq_printf(m, "rptr next(0x%04x): 0x%08x\n", ring->rptr_save_reg,
796 			   RREG32(ring->rptr_save_reg));
797 	}
798 	seq_printf(m, "driver's copy of the wptr: 0x%08x [%5d]\n", ring->wptr, ring->wptr);
799 	seq_printf(m, "driver's copy of the rptr: 0x%08x [%5d]\n", ring->rptr, ring->rptr);
800 	seq_printf(m, "last semaphore signal addr : 0x%016llx\n", ring->last_semaphore_signal_addr);
801 	seq_printf(m, "last semaphore wait addr   : 0x%016llx\n", ring->last_semaphore_wait_addr);
802 	seq_printf(m, "%u free dwords in ring\n", ring->ring_free_dw);
803 	seq_printf(m, "%u dwords in ring\n", count);
804 	/* print 8 dw before current rptr as often it's the last executed
805 	 * packet that is the root issue
806 	 */
807 	i = (ring->rptr + ring->ptr_mask + 1 - 32) & ring->ptr_mask;
808 	if (ring->ready) {
809 		for (j = 0; j <= (count + 32); j++) {
810 			seq_printf(m, "r[%5d]=0x%08x\n", i, ring->ring[i]);
811 			i = (i + 1) & ring->ptr_mask;
812 		}
813 	}
814 	return 0;
815 }
816 
817 static int radeon_ring_type_gfx_index = RADEON_RING_TYPE_GFX_INDEX;
818 static int cayman_ring_type_cp1_index = CAYMAN_RING_TYPE_CP1_INDEX;
819 static int cayman_ring_type_cp2_index = CAYMAN_RING_TYPE_CP2_INDEX;
820 static int radeon_ring_type_dma1_index = R600_RING_TYPE_DMA_INDEX;
821 static int radeon_ring_type_dma2_index = CAYMAN_RING_TYPE_DMA1_INDEX;
822 
823 static struct drm_info_list radeon_debugfs_ring_info_list[] = {
824 	{"radeon_ring_gfx", radeon_debugfs_ring_info, 0, &radeon_ring_type_gfx_index},
825 	{"radeon_ring_cp1", radeon_debugfs_ring_info, 0, &cayman_ring_type_cp1_index},
826 	{"radeon_ring_cp2", radeon_debugfs_ring_info, 0, &cayman_ring_type_cp2_index},
827 	{"radeon_ring_dma1", radeon_debugfs_ring_info, 0, &radeon_ring_type_dma1_index},
828 	{"radeon_ring_dma2", radeon_debugfs_ring_info, 0, &radeon_ring_type_dma2_index},
829 };
830 
831 static int radeon_debugfs_sa_info(struct seq_file *m, void *data)
832 {
833 	struct drm_info_node *node = (struct drm_info_node *) m->private;
834 	struct drm_device *dev = node->minor->dev;
835 	struct radeon_device *rdev = dev->dev_private;
836 
837 	radeon_sa_bo_dump_debug_info(&rdev->ring_tmp_bo, m);
838 
839 	return 0;
840 
841 }
842 
843 static struct drm_info_list radeon_debugfs_sa_list[] = {
844         {"radeon_sa_info", &radeon_debugfs_sa_info, 0, NULL},
845 };
846 
847 #endif
848 
849 static int radeon_debugfs_ring_init(struct radeon_device *rdev, struct radeon_ring *ring)
850 {
851 #if defined(CONFIG_DEBUG_FS)
852 	unsigned i;
853 	for (i = 0; i < ARRAY_SIZE(radeon_debugfs_ring_info_list); ++i) {
854 		struct drm_info_list *info = &radeon_debugfs_ring_info_list[i];
855 		int ridx = *(int*)radeon_debugfs_ring_info_list[i].data;
856 		unsigned r;
857 
858 		if (&rdev->ring[ridx] != ring)
859 			continue;
860 
861 		r = radeon_debugfs_add_files(rdev, info, 1);
862 		if (r)
863 			return r;
864 	}
865 #endif
866 	return 0;
867 }
868 
869 static int radeon_debugfs_sa_init(struct radeon_device *rdev)
870 {
871 #if defined(CONFIG_DEBUG_FS)
872 	return radeon_debugfs_add_files(rdev, radeon_debugfs_sa_list, 1);
873 #else
874 	return 0;
875 #endif
876 }
877