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