drm/radeon: update rptr saving logic for memory buffers
[linux-2.6-block.git] / drivers / gpu / 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  */
29 #include <linux/seq_file.h>
30 #include <linux/slab.h>
31 #include "drmP.h"
32 #include "radeon_drm.h"
33 #include "radeon_reg.h"
34 #include "radeon.h"
35 #include "atom.h"
36
37 /*
38  * IB.
39  */
40 int radeon_debugfs_sa_init(struct radeon_device *rdev);
41
42 int radeon_ib_get(struct radeon_device *rdev, int ring,
43                   struct radeon_ib *ib, unsigned size)
44 {
45         int i, r;
46
47         r = radeon_sa_bo_new(rdev, &rdev->ring_tmp_bo, &ib->sa_bo, size, 256, true);
48         if (r) {
49                 dev_err(rdev->dev, "failed to get a new IB (%d)\n", r);
50                 return r;
51         }
52
53         r = radeon_semaphore_create(rdev, &ib->semaphore);
54         if (r) {
55                 return r;
56         }
57
58         ib->ring = ring;
59         ib->fence = NULL;
60         ib->ptr = radeon_sa_bo_cpu_addr(ib->sa_bo);
61         ib->gpu_addr = radeon_sa_bo_gpu_addr(ib->sa_bo);
62         ib->vm_id = 0;
63         ib->is_const_ib = false;
64         for (i = 0; i < RADEON_NUM_RINGS; ++i)
65                 ib->sync_to[i] = NULL;
66
67         return 0;
68 }
69
70 void radeon_ib_free(struct radeon_device *rdev, struct radeon_ib *ib)
71 {
72         radeon_semaphore_free(rdev, &ib->semaphore, ib->fence);
73         radeon_sa_bo_free(rdev, &ib->sa_bo, ib->fence);
74         radeon_fence_unref(&ib->fence);
75 }
76
77 int radeon_ib_schedule(struct radeon_device *rdev, struct radeon_ib *ib,
78                        struct radeon_ib *const_ib)
79 {
80         struct radeon_ring *ring = &rdev->ring[ib->ring];
81         bool need_sync = false;
82         int i, r = 0;
83
84         if (!ib->length_dw || !ring->ready) {
85                 /* TODO: Nothings in the ib we should report. */
86                 dev_err(rdev->dev, "couldn't schedule ib\n");
87                 return -EINVAL;
88         }
89
90         /* 64 dwords should be enough for fence too */
91         r = radeon_ring_lock(rdev, ring, 64 + RADEON_NUM_RINGS * 8);
92         if (r) {
93                 dev_err(rdev->dev, "scheduling IB failed (%d).\n", r);
94                 return r;
95         }
96         for (i = 0; i < RADEON_NUM_RINGS; ++i) {
97                 struct radeon_fence *fence = ib->sync_to[i];
98                 if (radeon_fence_need_sync(fence, ib->ring)) {
99                         need_sync = true;
100                         radeon_semaphore_sync_rings(rdev, ib->semaphore,
101                                                     fence->ring, ib->ring);
102                         radeon_fence_note_sync(fence, ib->ring);
103                 }
104         }
105         /* immediately free semaphore when we don't need to sync */
106         if (!need_sync) {
107                 radeon_semaphore_free(rdev, &ib->semaphore, NULL);
108         }
109         if (const_ib) {
110                 radeon_ring_ib_execute(rdev, const_ib->ring, const_ib);
111                 radeon_semaphore_free(rdev, &const_ib->semaphore, NULL);
112         }
113         radeon_ring_ib_execute(rdev, ib->ring, ib);
114         r = radeon_fence_emit(rdev, &ib->fence, ib->ring);
115         if (r) {
116                 dev_err(rdev->dev, "failed to emit fence for new IB (%d)\n", r);
117                 radeon_ring_unlock_undo(rdev, ring);
118                 return r;
119         }
120         if (const_ib) {
121                 const_ib->fence = radeon_fence_ref(ib->fence);
122         }
123         radeon_ring_unlock_commit(rdev, ring);
124         return 0;
125 }
126
127 int radeon_ib_pool_init(struct radeon_device *rdev)
128 {
129         int r;
130
131         if (rdev->ib_pool_ready) {
132                 return 0;
133         }
134         r = radeon_sa_bo_manager_init(rdev, &rdev->ring_tmp_bo,
135                                       RADEON_IB_POOL_SIZE*64*1024,
136                                       RADEON_GEM_DOMAIN_GTT);
137         if (r) {
138                 return r;
139         }
140
141         r = radeon_sa_bo_manager_start(rdev, &rdev->ring_tmp_bo);
142         if (r) {
143                 return r;
144         }
145
146         rdev->ib_pool_ready = true;
147         if (radeon_debugfs_sa_init(rdev)) {
148                 dev_err(rdev->dev, "failed to register debugfs file for SA\n");
149         }
150         return 0;
151 }
152
153 void radeon_ib_pool_fini(struct radeon_device *rdev)
154 {
155         if (rdev->ib_pool_ready) {
156                 radeon_sa_bo_manager_suspend(rdev, &rdev->ring_tmp_bo);
157                 radeon_sa_bo_manager_fini(rdev, &rdev->ring_tmp_bo);
158                 rdev->ib_pool_ready = false;
159         }
160 }
161
162 int radeon_ib_ring_tests(struct radeon_device *rdev)
163 {
164         unsigned i;
165         int r;
166
167         for (i = 0; i < RADEON_NUM_RINGS; ++i) {
168                 struct radeon_ring *ring = &rdev->ring[i];
169
170                 if (!ring->ready)
171                         continue;
172
173                 r = radeon_ib_test(rdev, i, ring);
174                 if (r) {
175                         ring->ready = false;
176
177                         if (i == RADEON_RING_TYPE_GFX_INDEX) {
178                                 /* oh, oh, that's really bad */
179                                 DRM_ERROR("radeon: failed testing IB on GFX ring (%d).\n", r);
180                                 rdev->accel_working = false;
181                                 return r;
182
183                         } else {
184                                 /* still not good, but we can live with it */
185                                 DRM_ERROR("radeon: failed testing IB on ring %d (%d).\n", i, r);
186                         }
187                 }
188         }
189         return 0;
190 }
191
192 /*
193  * Ring.
194  */
195 int radeon_debugfs_ring_init(struct radeon_device *rdev, struct radeon_ring *ring);
196
197 void radeon_ring_write(struct radeon_ring *ring, uint32_t v)
198 {
199 #if DRM_DEBUG_CODE
200         if (ring->count_dw <= 0) {
201                 DRM_ERROR("radeon: writting more dword to ring than expected !\n");
202         }
203 #endif
204         ring->ring[ring->wptr++] = v;
205         ring->wptr &= ring->ptr_mask;
206         ring->count_dw--;
207         ring->ring_free_dw--;
208 }
209
210 bool radeon_ring_supports_scratch_reg(struct radeon_device *rdev,
211                                       struct radeon_ring *ring)
212 {
213         switch (ring->idx) {
214         case RADEON_RING_TYPE_GFX_INDEX:
215         case CAYMAN_RING_TYPE_CP1_INDEX:
216         case CAYMAN_RING_TYPE_CP2_INDEX:
217                 return true;
218         default:
219                 return false;
220         }
221 }
222
223 void radeon_ring_free_size(struct radeon_device *rdev, struct radeon_ring *ring)
224 {
225         u32 rptr;
226
227         if (rdev->wb.enabled)
228                 rptr = le32_to_cpu(rdev->wb.wb[ring->rptr_offs/4]);
229         else
230                 rptr = RREG32(ring->rptr_reg);
231         ring->rptr = (rptr & ring->ptr_reg_mask) >> ring->ptr_reg_shift;
232         /* This works because ring_size is a power of 2 */
233         ring->ring_free_dw = (ring->rptr + (ring->ring_size / 4));
234         ring->ring_free_dw -= ring->wptr;
235         ring->ring_free_dw &= ring->ptr_mask;
236         if (!ring->ring_free_dw) {
237                 ring->ring_free_dw = ring->ring_size / 4;
238         }
239 }
240
241
242 int radeon_ring_alloc(struct radeon_device *rdev, struct radeon_ring *ring, unsigned ndw)
243 {
244         int r;
245
246         /* Align requested size with padding so unlock_commit can
247          * pad safely */
248         ndw = (ndw + ring->align_mask) & ~ring->align_mask;
249         while (ndw > (ring->ring_free_dw - 1)) {
250                 radeon_ring_free_size(rdev, ring);
251                 if (ndw < ring->ring_free_dw) {
252                         break;
253                 }
254                 r = radeon_fence_wait_next_locked(rdev, ring->idx);
255                 if (r)
256                         return r;
257         }
258         ring->count_dw = ndw;
259         ring->wptr_old = ring->wptr;
260         return 0;
261 }
262
263 int radeon_ring_lock(struct radeon_device *rdev, struct radeon_ring *ring, unsigned ndw)
264 {
265         int r;
266
267         mutex_lock(&rdev->ring_lock);
268         r = radeon_ring_alloc(rdev, ring, ndw);
269         if (r) {
270                 mutex_unlock(&rdev->ring_lock);
271                 return r;
272         }
273         return 0;
274 }
275
276 void radeon_ring_commit(struct radeon_device *rdev, struct radeon_ring *ring)
277 {
278         /* We pad to match fetch size */
279         while (ring->wptr & ring->align_mask) {
280                 radeon_ring_write(ring, ring->nop);
281         }
282         DRM_MEMORYBARRIER();
283         WREG32(ring->wptr_reg, (ring->wptr << ring->ptr_reg_shift) & ring->ptr_reg_mask);
284         (void)RREG32(ring->wptr_reg);
285 }
286
287 void radeon_ring_unlock_commit(struct radeon_device *rdev, struct radeon_ring *ring)
288 {
289         radeon_ring_commit(rdev, ring);
290         mutex_unlock(&rdev->ring_lock);
291 }
292
293 void radeon_ring_undo(struct radeon_ring *ring)
294 {
295         ring->wptr = ring->wptr_old;
296 }
297
298 void radeon_ring_unlock_undo(struct radeon_device *rdev, struct radeon_ring *ring)
299 {
300         radeon_ring_undo(ring);
301         mutex_unlock(&rdev->ring_lock);
302 }
303
304 void radeon_ring_force_activity(struct radeon_device *rdev, struct radeon_ring *ring)
305 {
306         int r;
307
308         radeon_ring_free_size(rdev, ring);
309         if (ring->rptr == ring->wptr) {
310                 r = radeon_ring_alloc(rdev, ring, 1);
311                 if (!r) {
312                         radeon_ring_write(ring, ring->nop);
313                         radeon_ring_commit(rdev, ring);
314                 }
315         }
316 }
317
318 void radeon_ring_lockup_update(struct radeon_ring *ring)
319 {
320         ring->last_rptr = ring->rptr;
321         ring->last_activity = jiffies;
322 }
323
324 /**
325  * radeon_ring_test_lockup() - check if ring is lockedup by recording information
326  * @rdev:       radeon device structure
327  * @ring:       radeon_ring structure holding ring information
328  *
329  * We don't need to initialize the lockup tracking information as we will either
330  * have CP rptr to a different value of jiffies wrap around which will force
331  * initialization of the lockup tracking informations.
332  *
333  * A possible false positivie is if we get call after while and last_cp_rptr ==
334  * the current CP rptr, even if it's unlikely it might happen. To avoid this
335  * if the elapsed time since last call is bigger than 2 second than we return
336  * false and update the tracking information. Due to this the caller must call
337  * radeon_ring_test_lockup several time in less than 2sec for lockup to be reported
338  * the fencing code should be cautious about that.
339  *
340  * Caller should write to the ring to force CP to do something so we don't get
341  * false positive when CP is just gived nothing to do.
342  *
343  **/
344 bool radeon_ring_test_lockup(struct radeon_device *rdev, struct radeon_ring *ring)
345 {
346         unsigned long cjiffies, elapsed;
347         uint32_t rptr;
348
349         cjiffies = jiffies;
350         if (!time_after(cjiffies, ring->last_activity)) {
351                 /* likely a wrap around */
352                 radeon_ring_lockup_update(ring);
353                 return false;
354         }
355         rptr = RREG32(ring->rptr_reg);
356         ring->rptr = (rptr & ring->ptr_reg_mask) >> ring->ptr_reg_shift;
357         if (ring->rptr != ring->last_rptr) {
358                 /* CP is still working no lockup */
359                 radeon_ring_lockup_update(ring);
360                 return false;
361         }
362         elapsed = jiffies_to_msecs(cjiffies - ring->last_activity);
363         if (radeon_lockup_timeout && elapsed >= radeon_lockup_timeout) {
364                 dev_err(rdev->dev, "GPU lockup CP stall for more than %lumsec\n", elapsed);
365                 return true;
366         }
367         /* give a chance to the GPU ... */
368         return false;
369 }
370
371 /**
372  * radeon_ring_backup - Back up the content of a ring
373  *
374  * @rdev: radeon_device pointer
375  * @ring: the ring we want to back up
376  *
377  * Saves all unprocessed commits from a ring, returns the number of dwords saved.
378  */
379 unsigned radeon_ring_backup(struct radeon_device *rdev, struct radeon_ring *ring,
380                             uint32_t **data)
381 {
382         unsigned size, ptr, i;
383
384         /* just in case lock the ring */
385         mutex_lock(&rdev->ring_lock);
386         *data = NULL;
387
388         if (ring->ring_obj == NULL) {
389                 mutex_unlock(&rdev->ring_lock);
390                 return 0;
391         }
392
393         /* it doesn't make sense to save anything if all fences are signaled */
394         if (!radeon_fence_count_emitted(rdev, ring->idx)) {
395                 mutex_unlock(&rdev->ring_lock);
396                 return 0;
397         }
398
399         /* calculate the number of dw on the ring */
400         if (ring->rptr_save_reg)
401                 ptr = RREG32(ring->rptr_save_reg);
402         else if (rdev->wb.enabled)
403                 ptr = le32_to_cpu(*ring->next_rptr_cpu_addr);
404         else {
405                 /* no way to read back the next rptr */
406                 mutex_unlock(&rdev->ring_lock);
407                 return 0;
408         }
409
410         size = ring->wptr + (ring->ring_size / 4);
411         size -= ptr;
412         size &= ring->ptr_mask;
413         if (size == 0) {
414                 mutex_unlock(&rdev->ring_lock);
415                 return 0;
416         }
417
418         /* and then save the content of the ring */
419         *data = kmalloc(size * 4, GFP_KERNEL);
420         for (i = 0; i < size; ++i) {
421                 (*data)[i] = ring->ring[ptr++];
422                 ptr &= ring->ptr_mask;
423         }
424
425         mutex_unlock(&rdev->ring_lock);
426         return size;
427 }
428
429 /**
430  * radeon_ring_restore - append saved commands to the ring again
431  *
432  * @rdev: radeon_device pointer
433  * @ring: ring to append commands to
434  * @size: number of dwords we want to write
435  * @data: saved commands
436  *
437  * Allocates space on the ring and restore the previously saved commands.
438  */
439 int radeon_ring_restore(struct radeon_device *rdev, struct radeon_ring *ring,
440                         unsigned size, uint32_t *data)
441 {
442         int i, r;
443
444         if (!size || !data)
445                 return 0;
446
447         /* restore the saved ring content */
448         r = radeon_ring_lock(rdev, ring, size);
449         if (r)
450                 return r;
451
452         for (i = 0; i < size; ++i) {
453                 radeon_ring_write(ring, data[i]);
454         }
455
456         radeon_ring_unlock_commit(rdev, ring);
457         kfree(data);
458         return 0;
459 }
460
461 int radeon_ring_init(struct radeon_device *rdev, struct radeon_ring *ring, unsigned ring_size,
462                      unsigned rptr_offs, unsigned rptr_reg, unsigned wptr_reg,
463                      u32 ptr_reg_shift, u32 ptr_reg_mask, u32 nop)
464 {
465         int r;
466
467         ring->ring_size = ring_size;
468         ring->rptr_offs = rptr_offs;
469         ring->rptr_reg = rptr_reg;
470         ring->wptr_reg = wptr_reg;
471         ring->ptr_reg_shift = ptr_reg_shift;
472         ring->ptr_reg_mask = ptr_reg_mask;
473         ring->nop = nop;
474         /* Allocate ring buffer */
475         if (ring->ring_obj == NULL) {
476                 r = radeon_bo_create(rdev, ring->ring_size, PAGE_SIZE, true,
477                                      RADEON_GEM_DOMAIN_GTT,
478                                      NULL, &ring->ring_obj);
479                 if (r) {
480                         dev_err(rdev->dev, "(%d) ring create failed\n", r);
481                         return r;
482                 }
483                 r = radeon_bo_reserve(ring->ring_obj, false);
484                 if (unlikely(r != 0))
485                         return r;
486                 r = radeon_bo_pin(ring->ring_obj, RADEON_GEM_DOMAIN_GTT,
487                                         &ring->gpu_addr);
488                 if (r) {
489                         radeon_bo_unreserve(ring->ring_obj);
490                         dev_err(rdev->dev, "(%d) ring pin failed\n", r);
491                         return r;
492                 }
493                 r = radeon_bo_kmap(ring->ring_obj,
494                                        (void **)&ring->ring);
495                 radeon_bo_unreserve(ring->ring_obj);
496                 if (r) {
497                         dev_err(rdev->dev, "(%d) ring map failed\n", r);
498                         return r;
499                 }
500         }
501         ring->ptr_mask = (ring->ring_size / 4) - 1;
502         ring->ring_free_dw = ring->ring_size / 4;
503         if (rdev->wb.enabled) {
504                 u32 index = RADEON_WB_RING0_NEXT_RPTR + (ring->idx * 4);
505                 ring->next_rptr_gpu_addr = rdev->wb.gpu_addr + index;
506                 ring->next_rptr_cpu_addr = &rdev->wb.wb[index/4];
507         }
508         if (radeon_debugfs_ring_init(rdev, ring)) {
509                 DRM_ERROR("Failed to register debugfs file for rings !\n");
510         }
511         return 0;
512 }
513
514 void radeon_ring_fini(struct radeon_device *rdev, struct radeon_ring *ring)
515 {
516         int r;
517         struct radeon_bo *ring_obj;
518
519         mutex_lock(&rdev->ring_lock);
520         ring_obj = ring->ring_obj;
521         ring->ready = false;
522         ring->ring = NULL;
523         ring->ring_obj = NULL;
524         mutex_unlock(&rdev->ring_lock);
525
526         if (ring_obj) {
527                 r = radeon_bo_reserve(ring_obj, false);
528                 if (likely(r == 0)) {
529                         radeon_bo_kunmap(ring_obj);
530                         radeon_bo_unpin(ring_obj);
531                         radeon_bo_unreserve(ring_obj);
532                 }
533                 radeon_bo_unref(&ring_obj);
534         }
535 }
536
537 /*
538  * Debugfs info
539  */
540 #if defined(CONFIG_DEBUG_FS)
541
542 static int radeon_debugfs_ring_info(struct seq_file *m, void *data)
543 {
544         struct drm_info_node *node = (struct drm_info_node *) m->private;
545         struct drm_device *dev = node->minor->dev;
546         struct radeon_device *rdev = dev->dev_private;
547         int ridx = *(int*)node->info_ent->data;
548         struct radeon_ring *ring = &rdev->ring[ridx];
549         unsigned count, i, j;
550
551         radeon_ring_free_size(rdev, ring);
552         count = (ring->ring_size / 4) - ring->ring_free_dw;
553         seq_printf(m, "wptr(0x%04x): 0x%08x\n", ring->wptr_reg, RREG32(ring->wptr_reg));
554         seq_printf(m, "rptr(0x%04x): 0x%08x\n", ring->rptr_reg, RREG32(ring->rptr_reg));
555         if (ring->rptr_save_reg) {
556                 seq_printf(m, "rptr next(0x%04x): 0x%08x\n", ring->rptr_save_reg,
557                            RREG32(ring->rptr_save_reg));
558         }
559         seq_printf(m, "driver's copy of the wptr: 0x%08x\n", ring->wptr);
560         seq_printf(m, "driver's copy of the rptr: 0x%08x\n", ring->rptr);
561         seq_printf(m, "%u free dwords in ring\n", ring->ring_free_dw);
562         seq_printf(m, "%u dwords in ring\n", count);
563         i = ring->rptr;
564         for (j = 0; j <= count; j++) {
565                 seq_printf(m, "r[%04d]=0x%08x\n", i, ring->ring[i]);
566                 i = (i + 1) & ring->ptr_mask;
567         }
568         return 0;
569 }
570
571 static int radeon_ring_type_gfx_index = RADEON_RING_TYPE_GFX_INDEX;
572 static int cayman_ring_type_cp1_index = CAYMAN_RING_TYPE_CP1_INDEX;
573 static int cayman_ring_type_cp2_index = CAYMAN_RING_TYPE_CP2_INDEX;
574
575 static struct drm_info_list radeon_debugfs_ring_info_list[] = {
576         {"radeon_ring_gfx", radeon_debugfs_ring_info, 0, &radeon_ring_type_gfx_index},
577         {"radeon_ring_cp1", radeon_debugfs_ring_info, 0, &cayman_ring_type_cp1_index},
578         {"radeon_ring_cp2", radeon_debugfs_ring_info, 0, &cayman_ring_type_cp2_index},
579 };
580
581 static int radeon_debugfs_sa_info(struct seq_file *m, void *data)
582 {
583         struct drm_info_node *node = (struct drm_info_node *) m->private;
584         struct drm_device *dev = node->minor->dev;
585         struct radeon_device *rdev = dev->dev_private;
586
587         radeon_sa_bo_dump_debug_info(&rdev->ring_tmp_bo, m);
588
589         return 0;
590
591 }
592
593 static struct drm_info_list radeon_debugfs_sa_list[] = {
594         {"radeon_sa_info", &radeon_debugfs_sa_info, 0, NULL},
595 };
596
597 #endif
598
599 int radeon_debugfs_ring_init(struct radeon_device *rdev, struct radeon_ring *ring)
600 {
601 #if defined(CONFIG_DEBUG_FS)
602         unsigned i;
603         for (i = 0; i < ARRAY_SIZE(radeon_debugfs_ring_info_list); ++i) {
604                 struct drm_info_list *info = &radeon_debugfs_ring_info_list[i];
605                 int ridx = *(int*)radeon_debugfs_ring_info_list[i].data;
606                 unsigned r;
607
608                 if (&rdev->ring[ridx] != ring)
609                         continue;
610
611                 r = radeon_debugfs_add_files(rdev, info, 1);
612                 if (r)
613                         return r;
614         }
615 #endif
616         return 0;
617 }
618
619 int radeon_debugfs_sa_init(struct radeon_device *rdev)
620 {
621 #if defined(CONFIG_DEBUG_FS)
622         return radeon_debugfs_add_files(rdev, radeon_debugfs_sa_list, 1);
623 #else
624         return 0;
625 #endif
626 }