2 * Copyright 2008 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 * Copyright 2009 Jerome Glisse.
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 * Authors: Dave Airlie
29 #include <linux/seq_file.h>
30 #include <linux/slab.h>
32 #include "radeon_drm.h"
33 #include "radeon_reg.h"
40 int radeon_debugfs_sa_init(struct radeon_device *rdev);
42 int radeon_ib_get(struct radeon_device *rdev, int ring,
43 struct radeon_ib *ib, unsigned size)
47 r = radeon_sa_bo_new(rdev, &rdev->ring_tmp_bo, &ib->sa_bo, size, 256, true);
49 dev_err(rdev->dev, "failed to get a new IB (%d)\n", r);
53 r = radeon_semaphore_create(rdev, &ib->semaphore);
60 ib->ptr = radeon_sa_bo_cpu_addr(ib->sa_bo);
61 ib->gpu_addr = radeon_sa_bo_gpu_addr(ib->sa_bo);
63 ib->is_const_ib = false;
64 for (i = 0; i < RADEON_NUM_RINGS; ++i)
65 ib->sync_to[i] = NULL;
70 void radeon_ib_free(struct radeon_device *rdev, struct radeon_ib *ib)
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);
77 int radeon_ib_schedule(struct radeon_device *rdev, struct radeon_ib *ib,
78 struct radeon_ib *const_ib)
80 struct radeon_ring *ring = &rdev->ring[ib->ring];
81 bool need_sync = false;
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");
90 /* 64 dwords should be enough for fence too */
91 r = radeon_ring_lock(rdev, ring, 64 + RADEON_NUM_RINGS * 8);
93 dev_err(rdev->dev, "scheduling IB failed (%d).\n", r);
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)) {
100 radeon_semaphore_sync_rings(rdev, ib->semaphore,
101 fence->ring, ib->ring);
102 radeon_fence_note_sync(fence, ib->ring);
105 /* immediately free semaphore when we don't need to sync */
107 radeon_semaphore_free(rdev, &ib->semaphore, NULL);
110 radeon_ring_ib_execute(rdev, const_ib->ring, const_ib);
111 radeon_semaphore_free(rdev, &const_ib->semaphore, NULL);
113 radeon_ring_ib_execute(rdev, ib->ring, ib);
114 r = radeon_fence_emit(rdev, &ib->fence, ib->ring);
116 dev_err(rdev->dev, "failed to emit fence for new IB (%d)\n", r);
117 radeon_ring_unlock_undo(rdev, ring);
121 const_ib->fence = radeon_fence_ref(ib->fence);
123 radeon_ring_unlock_commit(rdev, ring);
127 int radeon_ib_pool_init(struct radeon_device *rdev)
131 if (rdev->ib_pool_ready) {
134 r = radeon_sa_bo_manager_init(rdev, &rdev->ring_tmp_bo,
135 RADEON_IB_POOL_SIZE*64*1024,
136 RADEON_GEM_DOMAIN_GTT);
141 r = radeon_sa_bo_manager_start(rdev, &rdev->ring_tmp_bo);
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");
153 void radeon_ib_pool_fini(struct radeon_device *rdev)
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;
162 int radeon_ib_ring_tests(struct radeon_device *rdev)
167 for (i = 0; i < RADEON_NUM_RINGS; ++i) {
168 struct radeon_ring *ring = &rdev->ring[i];
173 r = radeon_ib_test(rdev, i, ring);
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;
184 /* still not good, but we can live with it */
185 DRM_ERROR("radeon: failed testing IB on ring %d (%d).\n", i, r);
195 int radeon_debugfs_ring_init(struct radeon_device *rdev, struct radeon_ring *ring);
197 void radeon_ring_write(struct radeon_ring *ring, uint32_t v)
200 if (ring->count_dw <= 0) {
201 DRM_ERROR("radeon: writting more dword to ring than expected !\n");
204 ring->ring[ring->wptr++] = v;
205 ring->wptr &= ring->ptr_mask;
207 ring->ring_free_dw--;
210 bool radeon_ring_supports_scratch_reg(struct radeon_device *rdev,
211 struct radeon_ring *ring)
214 case RADEON_RING_TYPE_GFX_INDEX:
215 case CAYMAN_RING_TYPE_CP1_INDEX:
216 case CAYMAN_RING_TYPE_CP2_INDEX:
223 void radeon_ring_free_size(struct radeon_device *rdev, struct radeon_ring *ring)
227 if (rdev->wb.enabled)
228 rptr = le32_to_cpu(rdev->wb.wb[ring->rptr_offs/4]);
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;
242 int radeon_ring_alloc(struct radeon_device *rdev, struct radeon_ring *ring, unsigned ndw)
246 /* Align requested size with padding so unlock_commit can
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) {
254 r = radeon_fence_wait_next_locked(rdev, ring->idx);
258 ring->count_dw = ndw;
259 ring->wptr_old = ring->wptr;
263 int radeon_ring_lock(struct radeon_device *rdev, struct radeon_ring *ring, unsigned ndw)
267 mutex_lock(&rdev->ring_lock);
268 r = radeon_ring_alloc(rdev, ring, ndw);
270 mutex_unlock(&rdev->ring_lock);
276 void radeon_ring_commit(struct radeon_device *rdev, struct radeon_ring *ring)
278 /* We pad to match fetch size */
279 while (ring->wptr & ring->align_mask) {
280 radeon_ring_write(ring, ring->nop);
283 WREG32(ring->wptr_reg, (ring->wptr << ring->ptr_reg_shift) & ring->ptr_reg_mask);
284 (void)RREG32(ring->wptr_reg);
287 void radeon_ring_unlock_commit(struct radeon_device *rdev, struct radeon_ring *ring)
289 radeon_ring_commit(rdev, ring);
290 mutex_unlock(&rdev->ring_lock);
293 void radeon_ring_undo(struct radeon_ring *ring)
295 ring->wptr = ring->wptr_old;
298 void radeon_ring_unlock_undo(struct radeon_device *rdev, struct radeon_ring *ring)
300 radeon_ring_undo(ring);
301 mutex_unlock(&rdev->ring_lock);
304 void radeon_ring_force_activity(struct radeon_device *rdev, struct radeon_ring *ring)
308 radeon_ring_free_size(rdev, ring);
309 if (ring->rptr == ring->wptr) {
310 r = radeon_ring_alloc(rdev, ring, 1);
312 radeon_ring_write(ring, ring->nop);
313 radeon_ring_commit(rdev, ring);
318 void radeon_ring_lockup_update(struct radeon_ring *ring)
320 ring->last_rptr = ring->rptr;
321 ring->last_activity = jiffies;
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
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.
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.
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.
344 bool radeon_ring_test_lockup(struct radeon_device *rdev, struct radeon_ring *ring)
346 unsigned long cjiffies, elapsed;
350 if (!time_after(cjiffies, ring->last_activity)) {
351 /* likely a wrap around */
352 radeon_ring_lockup_update(ring);
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);
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);
367 /* give a chance to the GPU ... */
372 * radeon_ring_backup - Back up the content of a ring
374 * @rdev: radeon_device pointer
375 * @ring: the ring we want to back up
377 * Saves all unprocessed commits from a ring, returns the number of dwords saved.
379 unsigned radeon_ring_backup(struct radeon_device *rdev, struct radeon_ring *ring,
382 unsigned size, ptr, i;
384 /* just in case lock the ring */
385 mutex_lock(&rdev->ring_lock);
388 if (ring->ring_obj == NULL) {
389 mutex_unlock(&rdev->ring_lock);
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);
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);
405 /* no way to read back the next rptr */
406 mutex_unlock(&rdev->ring_lock);
410 size = ring->wptr + (ring->ring_size / 4);
412 size &= ring->ptr_mask;
414 mutex_unlock(&rdev->ring_lock);
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;
425 mutex_unlock(&rdev->ring_lock);
430 * radeon_ring_restore - append saved commands to the ring again
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
437 * Allocates space on the ring and restore the previously saved commands.
439 int radeon_ring_restore(struct radeon_device *rdev, struct radeon_ring *ring,
440 unsigned size, uint32_t *data)
447 /* restore the saved ring content */
448 r = radeon_ring_lock(rdev, ring, size);
452 for (i = 0; i < size; ++i) {
453 radeon_ring_write(ring, data[i]);
456 radeon_ring_unlock_commit(rdev, ring);
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)
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;
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);
480 dev_err(rdev->dev, "(%d) ring create failed\n", r);
483 r = radeon_bo_reserve(ring->ring_obj, false);
484 if (unlikely(r != 0))
486 r = radeon_bo_pin(ring->ring_obj, RADEON_GEM_DOMAIN_GTT,
489 radeon_bo_unreserve(ring->ring_obj);
490 dev_err(rdev->dev, "(%d) ring pin failed\n", r);
493 r = radeon_bo_kmap(ring->ring_obj,
494 (void **)&ring->ring);
495 radeon_bo_unreserve(ring->ring_obj);
497 dev_err(rdev->dev, "(%d) ring map failed\n", r);
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];
508 if (radeon_debugfs_ring_init(rdev, ring)) {
509 DRM_ERROR("Failed to register debugfs file for rings !\n");
514 void radeon_ring_fini(struct radeon_device *rdev, struct radeon_ring *ring)
517 struct radeon_bo *ring_obj;
519 mutex_lock(&rdev->ring_lock);
520 ring_obj = ring->ring_obj;
523 ring->ring_obj = NULL;
524 mutex_unlock(&rdev->ring_lock);
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);
533 radeon_bo_unref(&ring_obj);
540 #if defined(CONFIG_DEBUG_FS)
542 static int radeon_debugfs_ring_info(struct seq_file *m, void *data)
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;
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));
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);
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;
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;
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},
581 static int radeon_debugfs_sa_info(struct seq_file *m, void *data)
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;
587 radeon_sa_bo_dump_debug_info(&rdev->ring_tmp_bo, m);
593 static struct drm_info_list radeon_debugfs_sa_list[] = {
594 {"radeon_sa_info", &radeon_debugfs_sa_info, 0, NULL},
599 int radeon_debugfs_ring_init(struct radeon_device *rdev, struct radeon_ring *ring)
601 #if defined(CONFIG_DEBUG_FS)
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;
608 if (&rdev->ring[ridx] != ring)
611 r = radeon_debugfs_add_files(rdev, info, 1);
619 int radeon_debugfs_sa_init(struct radeon_device *rdev)
621 #if defined(CONFIG_DEBUG_FS)
622 return radeon_debugfs_add_files(rdev, radeon_debugfs_sa_list, 1);