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
30 #include <drm/drm_debugfs.h>
31 #include <drm/drm_device.h>
32 #include <drm/drm_file.h>
38 * Most engines on the GPU are fed via ring buffers. Ring
39 * buffers are areas of GPU accessible memory that the host
40 * writes commands into and the GPU reads commands out of.
41 * There is a rptr (read pointer) that determines where the
42 * GPU is currently reading, and a wptr (write pointer)
43 * which determines where the host has written. When the
44 * pointers are equal, the ring is idle. When the host
45 * writes commands to the ring buffer, it increments the
46 * wptr. The GPU then starts fetching commands and executes
47 * them until the pointers are equal again.
49 static int radeon_debugfs_ring_init(struct radeon_device
*rdev
, struct radeon_ring
*ring
);
52 * radeon_ring_supports_scratch_reg - check if the ring supports
53 * writing to scratch registers
55 * @rdev: radeon_device pointer
56 * @ring: radeon_ring structure holding ring information
58 * Check if a specific ring supports writing to scratch registers (all asics).
59 * Returns true if the ring supports writing to scratch regs, false if not.
61 bool radeon_ring_supports_scratch_reg(struct radeon_device
*rdev
,
62 struct radeon_ring
*ring
)
65 case RADEON_RING_TYPE_GFX_INDEX
:
66 case CAYMAN_RING_TYPE_CP1_INDEX
:
67 case CAYMAN_RING_TYPE_CP2_INDEX
:
75 * radeon_ring_free_size - update the free size
77 * @rdev: radeon_device pointer
78 * @ring: radeon_ring structure holding ring information
80 * Update the free dw slots in the ring buffer (all asics).
82 void radeon_ring_free_size(struct radeon_device
*rdev
, struct radeon_ring
*ring
)
84 uint32_t rptr
= radeon_ring_get_rptr(rdev
, ring
);
86 /* This works because ring_size is a power of 2 */
87 ring
->ring_free_dw
= rptr
+ (ring
->ring_size
/ 4);
88 ring
->ring_free_dw
-= ring
->wptr
;
89 ring
->ring_free_dw
&= ring
->ptr_mask
;
90 if (!ring
->ring_free_dw
) {
91 /* this is an empty ring */
92 ring
->ring_free_dw
= ring
->ring_size
/ 4;
93 /* update lockup info to avoid false positive */
94 radeon_ring_lockup_update(rdev
, ring
);
99 * radeon_ring_alloc - allocate space on the ring buffer
101 * @rdev: radeon_device pointer
102 * @ring: radeon_ring structure holding ring information
103 * @ndw: number of dwords to allocate in the ring buffer
105 * Allocate @ndw dwords in the ring buffer (all asics).
106 * Returns 0 on success, error on failure.
108 int radeon_ring_alloc(struct radeon_device
*rdev
, struct radeon_ring
*ring
, unsigned ndw
)
112 /* make sure we aren't trying to allocate more space than there is on the ring */
113 if (ndw
> (ring
->ring_size
/ 4))
115 /* Align requested size with padding so unlock_commit can
117 radeon_ring_free_size(rdev
, ring
);
118 ndw
= (ndw
+ ring
->align_mask
) & ~ring
->align_mask
;
119 while (ndw
> (ring
->ring_free_dw
- 1)) {
120 radeon_ring_free_size(rdev
, ring
);
121 if (ndw
< ring
->ring_free_dw
) {
124 r
= radeon_fence_wait_next(rdev
, ring
->idx
);
128 ring
->count_dw
= ndw
;
129 ring
->wptr_old
= ring
->wptr
;
134 * radeon_ring_lock - lock the ring and allocate space on it
136 * @rdev: radeon_device pointer
137 * @ring: radeon_ring structure holding ring information
138 * @ndw: number of dwords to allocate in the ring buffer
140 * Lock the ring and allocate @ndw dwords in the ring buffer
142 * Returns 0 on success, error on failure.
144 int radeon_ring_lock(struct radeon_device
*rdev
, struct radeon_ring
*ring
, unsigned ndw
)
148 mutex_lock(&rdev
->ring_lock
);
149 r
= radeon_ring_alloc(rdev
, ring
, ndw
);
151 mutex_unlock(&rdev
->ring_lock
);
158 * radeon_ring_commit - tell the GPU to execute the new
159 * commands on the ring buffer
161 * @rdev: radeon_device pointer
162 * @ring: radeon_ring structure holding ring information
163 * @hdp_flush: Whether or not to perform an HDP cache flush
165 * Update the wptr (write pointer) to tell the GPU to
166 * execute new commands on the ring buffer (all asics).
168 void radeon_ring_commit(struct radeon_device
*rdev
, struct radeon_ring
*ring
,
171 /* If we are emitting the HDP flush via the ring buffer, we need to
172 * do it before padding.
174 if (hdp_flush
&& rdev
->asic
->ring
[ring
->idx
]->hdp_flush
)
175 rdev
->asic
->ring
[ring
->idx
]->hdp_flush(rdev
, ring
);
176 /* We pad to match fetch size */
177 while (ring
->wptr
& ring
->align_mask
) {
178 radeon_ring_write(ring
, ring
->nop
);
181 /* If we are emitting the HDP flush via MMIO, we need to do it after
182 * all CPU writes to VRAM finished.
184 if (hdp_flush
&& rdev
->asic
->mmio_hdp_flush
)
185 rdev
->asic
->mmio_hdp_flush(rdev
);
186 radeon_ring_set_wptr(rdev
, ring
);
190 * radeon_ring_unlock_commit - tell the GPU to execute the new
191 * commands on the ring buffer and unlock it
193 * @rdev: radeon_device pointer
194 * @ring: radeon_ring structure holding ring information
195 * @hdp_flush: Whether or not to perform an HDP cache flush
197 * Call radeon_ring_commit() then unlock the ring (all asics).
199 void radeon_ring_unlock_commit(struct radeon_device
*rdev
, struct radeon_ring
*ring
,
202 radeon_ring_commit(rdev
, ring
, hdp_flush
);
203 mutex_unlock(&rdev
->ring_lock
);
207 * radeon_ring_undo - reset the wptr
209 * @ring: radeon_ring structure holding ring information
211 * Reset the driver's copy of the wptr (all asics).
213 void radeon_ring_undo(struct radeon_ring
*ring
)
215 ring
->wptr
= ring
->wptr_old
;
219 * radeon_ring_unlock_undo - reset the wptr and unlock the ring
221 * @ring: radeon_ring structure holding ring information
223 * Call radeon_ring_undo() then unlock the ring (all asics).
225 void radeon_ring_unlock_undo(struct radeon_device
*rdev
, struct radeon_ring
*ring
)
227 radeon_ring_undo(ring
);
228 mutex_unlock(&rdev
->ring_lock
);
232 * radeon_ring_lockup_update - update lockup variables
234 * @ring: radeon_ring structure holding ring information
236 * Update the last rptr value and timestamp (all asics).
238 void radeon_ring_lockup_update(struct radeon_device
*rdev
,
239 struct radeon_ring
*ring
)
241 atomic_set(&ring
->last_rptr
, radeon_ring_get_rptr(rdev
, ring
));
242 atomic64_set(&ring
->last_activity
, jiffies_64
);
246 * radeon_ring_test_lockup() - check if ring is lockedup by recording information
247 * @rdev: radeon device structure
248 * @ring: radeon_ring structure holding ring information
251 bool radeon_ring_test_lockup(struct radeon_device
*rdev
, struct radeon_ring
*ring
)
253 uint32_t rptr
= radeon_ring_get_rptr(rdev
, ring
);
254 uint64_t last
= atomic64_read(&ring
->last_activity
);
257 if (rptr
!= atomic_read(&ring
->last_rptr
)) {
258 /* ring is still working, no lockup */
259 radeon_ring_lockup_update(rdev
, ring
);
263 elapsed
= jiffies_to_msecs(jiffies_64
- last
);
264 if (radeon_lockup_timeout
&& elapsed
>= radeon_lockup_timeout
) {
265 dev_err(rdev
->dev
, "ring %d stalled for more than %llumsec\n",
269 /* give a chance to the GPU ... */
274 * radeon_ring_backup - Back up the content of a ring
276 * @rdev: radeon_device pointer
277 * @ring: the ring we want to back up
279 * Saves all unprocessed commits from a ring, returns the number of dwords saved.
281 unsigned radeon_ring_backup(struct radeon_device
*rdev
, struct radeon_ring
*ring
,
284 unsigned size
, ptr
, i
;
286 /* just in case lock the ring */
287 mutex_lock(&rdev
->ring_lock
);
290 if (ring
->ring_obj
== NULL
) {
291 mutex_unlock(&rdev
->ring_lock
);
295 /* it doesn't make sense to save anything if all fences are signaled */
296 if (!radeon_fence_count_emitted(rdev
, ring
->idx
)) {
297 mutex_unlock(&rdev
->ring_lock
);
301 /* calculate the number of dw on the ring */
302 if (ring
->rptr_save_reg
)
303 ptr
= RREG32(ring
->rptr_save_reg
);
304 else if (rdev
->wb
.enabled
)
305 ptr
= le32_to_cpu(*ring
->next_rptr_cpu_addr
);
307 /* no way to read back the next rptr */
308 mutex_unlock(&rdev
->ring_lock
);
312 size
= ring
->wptr
+ (ring
->ring_size
/ 4);
314 size
&= ring
->ptr_mask
;
316 mutex_unlock(&rdev
->ring_lock
);
320 /* and then save the content of the ring */
321 *data
= kvmalloc_array(size
, sizeof(uint32_t), GFP_KERNEL
);
323 mutex_unlock(&rdev
->ring_lock
);
326 for (i
= 0; i
< size
; ++i
) {
327 (*data
)[i
] = ring
->ring
[ptr
++];
328 ptr
&= ring
->ptr_mask
;
331 mutex_unlock(&rdev
->ring_lock
);
336 * radeon_ring_restore - append saved commands to the ring again
338 * @rdev: radeon_device pointer
339 * @ring: ring to append commands to
340 * @size: number of dwords we want to write
341 * @data: saved commands
343 * Allocates space on the ring and restore the previously saved commands.
345 int radeon_ring_restore(struct radeon_device
*rdev
, struct radeon_ring
*ring
,
346 unsigned size
, uint32_t *data
)
353 /* restore the saved ring content */
354 r
= radeon_ring_lock(rdev
, ring
, size
);
358 for (i
= 0; i
< size
; ++i
) {
359 radeon_ring_write(ring
, data
[i
]);
362 radeon_ring_unlock_commit(rdev
, ring
, false);
368 * radeon_ring_init - init driver ring struct.
370 * @rdev: radeon_device pointer
371 * @ring: radeon_ring structure holding ring information
372 * @ring_size: size of the ring
373 * @rptr_offs: offset of the rptr writeback location in the WB buffer
374 * @nop: nop packet for this ring
376 * Initialize the driver information for the selected ring (all asics).
377 * Returns 0 on success, error on failure.
379 int radeon_ring_init(struct radeon_device
*rdev
, struct radeon_ring
*ring
, unsigned ring_size
,
380 unsigned rptr_offs
, u32 nop
)
384 ring
->ring_size
= ring_size
;
385 ring
->rptr_offs
= rptr_offs
;
387 /* Allocate ring buffer */
388 if (ring
->ring_obj
== NULL
) {
389 r
= radeon_bo_create(rdev
, ring
->ring_size
, PAGE_SIZE
, true,
390 RADEON_GEM_DOMAIN_GTT
, 0, NULL
,
391 NULL
, &ring
->ring_obj
);
393 dev_err(rdev
->dev
, "(%d) ring create failed\n", r
);
396 r
= radeon_bo_reserve(ring
->ring_obj
, false);
397 if (unlikely(r
!= 0))
399 r
= radeon_bo_pin(ring
->ring_obj
, RADEON_GEM_DOMAIN_GTT
,
402 radeon_bo_unreserve(ring
->ring_obj
);
403 dev_err(rdev
->dev
, "(%d) ring pin failed\n", r
);
406 r
= radeon_bo_kmap(ring
->ring_obj
,
407 (void **)&ring
->ring
);
408 radeon_bo_unreserve(ring
->ring_obj
);
410 dev_err(rdev
->dev
, "(%d) ring map failed\n", r
);
414 ring
->ptr_mask
= (ring
->ring_size
/ 4) - 1;
415 ring
->ring_free_dw
= ring
->ring_size
/ 4;
416 if (rdev
->wb
.enabled
) {
417 u32 index
= RADEON_WB_RING0_NEXT_RPTR
+ (ring
->idx
* 4);
418 ring
->next_rptr_gpu_addr
= rdev
->wb
.gpu_addr
+ index
;
419 ring
->next_rptr_cpu_addr
= &rdev
->wb
.wb
[index
/4];
421 if (radeon_debugfs_ring_init(rdev
, ring
)) {
422 DRM_ERROR("Failed to register debugfs file for rings !\n");
424 radeon_ring_lockup_update(rdev
, ring
);
429 * radeon_ring_fini - tear down the driver ring struct.
431 * @rdev: radeon_device pointer
432 * @ring: radeon_ring structure holding ring information
434 * Tear down the driver information for the selected ring (all asics).
436 void radeon_ring_fini(struct radeon_device
*rdev
, struct radeon_ring
*ring
)
439 struct radeon_bo
*ring_obj
;
441 mutex_lock(&rdev
->ring_lock
);
442 ring_obj
= ring
->ring_obj
;
445 ring
->ring_obj
= NULL
;
446 mutex_unlock(&rdev
->ring_lock
);
449 r
= radeon_bo_reserve(ring_obj
, false);
450 if (likely(r
== 0)) {
451 radeon_bo_kunmap(ring_obj
);
452 radeon_bo_unpin(ring_obj
);
453 radeon_bo_unreserve(ring_obj
);
455 radeon_bo_unref(&ring_obj
);
462 #if defined(CONFIG_DEBUG_FS)
464 static int radeon_debugfs_ring_info(struct seq_file
*m
, void *data
)
466 struct drm_info_node
*node
= (struct drm_info_node
*) m
->private;
467 struct drm_device
*dev
= node
->minor
->dev
;
468 struct radeon_device
*rdev
= dev
->dev_private
;
469 int ridx
= *(int*)node
->info_ent
->data
;
470 struct radeon_ring
*ring
= &rdev
->ring
[ridx
];
472 uint32_t rptr
, wptr
, rptr_next
;
473 unsigned count
, i
, j
;
475 radeon_ring_free_size(rdev
, ring
);
476 count
= (ring
->ring_size
/ 4) - ring
->ring_free_dw
;
478 wptr
= radeon_ring_get_wptr(rdev
, ring
);
479 seq_printf(m
, "wptr: 0x%08x [%5d]\n",
482 rptr
= radeon_ring_get_rptr(rdev
, ring
);
483 seq_printf(m
, "rptr: 0x%08x [%5d]\n",
486 if (ring
->rptr_save_reg
) {
487 rptr_next
= RREG32(ring
->rptr_save_reg
);
488 seq_printf(m
, "rptr next(0x%04x): 0x%08x [%5d]\n",
489 ring
->rptr_save_reg
, rptr_next
, rptr_next
);
493 seq_printf(m
, "driver's copy of the wptr: 0x%08x [%5d]\n",
494 ring
->wptr
, ring
->wptr
);
495 seq_printf(m
, "last semaphore signal addr : 0x%016llx\n",
496 ring
->last_semaphore_signal_addr
);
497 seq_printf(m
, "last semaphore wait addr : 0x%016llx\n",
498 ring
->last_semaphore_wait_addr
);
499 seq_printf(m
, "%u free dwords in ring\n", ring
->ring_free_dw
);
500 seq_printf(m
, "%u dwords in ring\n", count
);
505 /* print 8 dw before current rptr as often it's the last executed
506 * packet that is the root issue
508 i
= (rptr
+ ring
->ptr_mask
+ 1 - 32) & ring
->ptr_mask
;
509 for (j
= 0; j
<= (count
+ 32); j
++) {
510 seq_printf(m
, "r[%5d]=0x%08x", i
, ring
->ring
[i
]);
516 i
= (i
+ 1) & ring
->ptr_mask
;
521 static int radeon_gfx_index
= RADEON_RING_TYPE_GFX_INDEX
;
522 static int cayman_cp1_index
= CAYMAN_RING_TYPE_CP1_INDEX
;
523 static int cayman_cp2_index
= CAYMAN_RING_TYPE_CP2_INDEX
;
524 static int radeon_dma1_index
= R600_RING_TYPE_DMA_INDEX
;
525 static int radeon_dma2_index
= CAYMAN_RING_TYPE_DMA1_INDEX
;
526 static int r600_uvd_index
= R600_RING_TYPE_UVD_INDEX
;
527 static int si_vce1_index
= TN_RING_TYPE_VCE1_INDEX
;
528 static int si_vce2_index
= TN_RING_TYPE_VCE2_INDEX
;
530 static struct drm_info_list radeon_debugfs_ring_info_list
[] = {
531 {"radeon_ring_gfx", radeon_debugfs_ring_info
, 0, &radeon_gfx_index
},
532 {"radeon_ring_cp1", radeon_debugfs_ring_info
, 0, &cayman_cp1_index
},
533 {"radeon_ring_cp2", radeon_debugfs_ring_info
, 0, &cayman_cp2_index
},
534 {"radeon_ring_dma1", radeon_debugfs_ring_info
, 0, &radeon_dma1_index
},
535 {"radeon_ring_dma2", radeon_debugfs_ring_info
, 0, &radeon_dma2_index
},
536 {"radeon_ring_uvd", radeon_debugfs_ring_info
, 0, &r600_uvd_index
},
537 {"radeon_ring_vce1", radeon_debugfs_ring_info
, 0, &si_vce1_index
},
538 {"radeon_ring_vce2", radeon_debugfs_ring_info
, 0, &si_vce2_index
},
543 static int radeon_debugfs_ring_init(struct radeon_device
*rdev
, struct radeon_ring
*ring
)
545 #if defined(CONFIG_DEBUG_FS)
547 for (i
= 0; i
< ARRAY_SIZE(radeon_debugfs_ring_info_list
); ++i
) {
548 struct drm_info_list
*info
= &radeon_debugfs_ring_info_list
[i
];
549 int ridx
= *(int*)radeon_debugfs_ring_info_list
[i
].data
;
552 if (&rdev
->ring
[ridx
] != ring
)
555 r
= radeon_debugfs_add_files(rdev
, info
, 1);