blob: 8c7872339c2a6f5e94bf601c47ed421e5caee352 [file] [log] [blame]
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001/*
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
Jerome Glissec507f7e2012-05-09 15:34:58 +020027 * Christian König
Jerome Glisse771fe6b2009-06-05 14:42:42 +020028 */
David Howells760285e2012-10-02 18:01:07 +010029#include <drm/drmP.h>
Jerome Glisse771fe6b2009-06-05 14:42:42 +020030#include "radeon.h"
Christian König7bd560e2012-05-02 15:11:12 +020031
Jerome Glisse771fe6b2009-06-05 14:42:42 +020032/*
Alex Deucher75923282012-07-17 14:02:38 -040033 * Rings
34 * Most engines on the GPU are fed via ring buffers. Ring
35 * buffers are areas of GPU accessible memory that the host
36 * writes commands into and the GPU reads commands out of.
37 * There is a rptr (read pointer) that determines where the
38 * GPU is currently reading, and a wptr (write pointer)
39 * which determines where the host has written. When the
40 * pointers are equal, the ring is idle. When the host
41 * writes commands to the ring buffer, it increments the
42 * wptr. The GPU then starts fetching commands and executes
43 * them until the pointers are equal again.
Jerome Glisse771fe6b2009-06-05 14:42:42 +020044 */
Lauri Kasanen1109ca02012-08-31 13:43:50 -040045static int radeon_debugfs_ring_init(struct radeon_device *rdev, struct radeon_ring *ring);
Jerome Glissec507f7e2012-05-09 15:34:58 +020046
Alex Deucher75923282012-07-17 14:02:38 -040047/**
Alex Deucher75923282012-07-17 14:02:38 -040048 * radeon_ring_supports_scratch_reg - check if the ring supports
49 * writing to scratch registers
50 *
51 * @rdev: radeon_device pointer
52 * @ring: radeon_ring structure holding ring information
53 *
54 * Check if a specific ring supports writing to scratch registers (all asics).
55 * Returns true if the ring supports writing to scratch regs, false if not.
56 */
Alex Deucher89d35802012-07-17 14:02:31 -040057bool radeon_ring_supports_scratch_reg(struct radeon_device *rdev,
58 struct radeon_ring *ring)
59{
60 switch (ring->idx) {
61 case RADEON_RING_TYPE_GFX_INDEX:
62 case CAYMAN_RING_TYPE_CP1_INDEX:
63 case CAYMAN_RING_TYPE_CP2_INDEX:
64 return true;
65 default:
66 return false;
67 }
68}
69
Alex Deucher75923282012-07-17 14:02:38 -040070/**
71 * radeon_ring_free_size - update the free size
72 *
73 * @rdev: radeon_device pointer
74 * @ring: radeon_ring structure holding ring information
75 *
76 * Update the free dw slots in the ring buffer (all asics).
77 */
Christian Könige32eb502011-10-23 12:56:27 +020078void radeon_ring_free_size(struct radeon_device *rdev, struct radeon_ring *ring)
Jerome Glisse771fe6b2009-06-05 14:42:42 +020079{
Christian Königff212f22014-02-18 14:52:33 +010080 uint32_t rptr = radeon_ring_get_rptr(rdev, ring);
81
Jerome Glisse771fe6b2009-06-05 14:42:42 +020082 /* This works because ring_size is a power of 2 */
Christian Königff212f22014-02-18 14:52:33 +010083 ring->ring_free_dw = rptr + (ring->ring_size / 4);
Christian Könige32eb502011-10-23 12:56:27 +020084 ring->ring_free_dw -= ring->wptr;
85 ring->ring_free_dw &= ring->ptr_mask;
86 if (!ring->ring_free_dw) {
Christian König82dc62a2014-02-18 15:03:22 +010087 /* this is an empty ring */
Christian Könige32eb502011-10-23 12:56:27 +020088 ring->ring_free_dw = ring->ring_size / 4;
Christian König82dc62a2014-02-18 15:03:22 +010089 /* update lockup info to avoid false positive */
90 radeon_ring_lockup_update(rdev, ring);
Jerome Glisse771fe6b2009-06-05 14:42:42 +020091 }
92}
93
Alex Deucher75923282012-07-17 14:02:38 -040094/**
95 * radeon_ring_alloc - allocate space on the ring buffer
96 *
97 * @rdev: radeon_device pointer
98 * @ring: radeon_ring structure holding ring information
99 * @ndw: number of dwords to allocate in the ring buffer
100 *
101 * Allocate @ndw dwords in the ring buffer (all asics).
102 * Returns 0 on success, error on failure.
103 */
Christian Könige32eb502011-10-23 12:56:27 +0200104int radeon_ring_alloc(struct radeon_device *rdev, struct radeon_ring *ring, unsigned ndw)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200105{
106 int r;
107
Alex Deucherfd5d93a2013-01-30 14:24:09 -0500108 /* make sure we aren't trying to allocate more space than there is on the ring */
109 if (ndw > (ring->ring_size / 4))
110 return -ENOMEM;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200111 /* Align requested size with padding so unlock_commit can
112 * pad safely */
Jerome Glisse8444d5c2013-06-19 10:02:28 -0400113 radeon_ring_free_size(rdev, ring);
Christian Könige32eb502011-10-23 12:56:27 +0200114 ndw = (ndw + ring->align_mask) & ~ring->align_mask;
115 while (ndw > (ring->ring_free_dw - 1)) {
116 radeon_ring_free_size(rdev, ring);
117 if (ndw < ring->ring_free_dw) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200118 break;
119 }
Christian König37615522014-02-18 15:58:31 +0100120 r = radeon_fence_wait_next(rdev, ring->idx);
Matthew Garrett91700f32010-04-30 15:24:17 -0400121 if (r)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200122 return r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200123 }
Christian Könige32eb502011-10-23 12:56:27 +0200124 ring->count_dw = ndw;
125 ring->wptr_old = ring->wptr;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200126 return 0;
127}
128
Alex Deucher75923282012-07-17 14:02:38 -0400129/**
130 * radeon_ring_lock - lock the ring and allocate space on it
131 *
132 * @rdev: radeon_device pointer
133 * @ring: radeon_ring structure holding ring information
134 * @ndw: number of dwords to allocate in the ring buffer
135 *
136 * Lock the ring and allocate @ndw dwords in the ring buffer
137 * (all asics).
138 * Returns 0 on success, error on failure.
139 */
Christian Könige32eb502011-10-23 12:56:27 +0200140int radeon_ring_lock(struct radeon_device *rdev, struct radeon_ring *ring, unsigned ndw)
Matthew Garrett91700f32010-04-30 15:24:17 -0400141{
142 int r;
143
Christian Königd6999bc2012-05-09 15:34:45 +0200144 mutex_lock(&rdev->ring_lock);
Christian Könige32eb502011-10-23 12:56:27 +0200145 r = radeon_ring_alloc(rdev, ring, ndw);
Matthew Garrett91700f32010-04-30 15:24:17 -0400146 if (r) {
Christian Königd6999bc2012-05-09 15:34:45 +0200147 mutex_unlock(&rdev->ring_lock);
Matthew Garrett91700f32010-04-30 15:24:17 -0400148 return r;
149 }
150 return 0;
151}
152
Alex Deucher75923282012-07-17 14:02:38 -0400153/**
154 * radeon_ring_commit - tell the GPU to execute the new
155 * commands on the ring buffer
156 *
157 * @rdev: radeon_device pointer
158 * @ring: radeon_ring structure holding ring information
Michel Dänzer1538a9e2014-08-18 17:34:55 +0900159 * @hdp_flush: Whether or not to perform an HDP cache flush
Alex Deucher75923282012-07-17 14:02:38 -0400160 *
161 * Update the wptr (write pointer) to tell the GPU to
162 * execute new commands on the ring buffer (all asics).
163 */
Michel Dänzer1538a9e2014-08-18 17:34:55 +0900164void radeon_ring_commit(struct radeon_device *rdev, struct radeon_ring *ring,
165 bool hdp_flush)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200166{
Michel Dänzer72a99872014-07-31 18:43:49 +0900167 /* If we are emitting the HDP flush via the ring buffer, we need to
168 * do it before padding.
169 */
Michel Dänzer1538a9e2014-08-18 17:34:55 +0900170 if (hdp_flush && rdev->asic->ring[ring->idx]->hdp_flush)
Michel Dänzer72a99872014-07-31 18:43:49 +0900171 rdev->asic->ring[ring->idx]->hdp_flush(rdev, ring);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200172 /* We pad to match fetch size */
Christian König07a71332012-07-07 12:11:32 +0200173 while (ring->wptr & ring->align_mask) {
Alex Deucher78c55602011-11-17 14:25:56 -0500174 radeon_ring_write(ring, ring->nop);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200175 }
Daniel Vetter85b23312013-12-11 11:34:45 +0100176 mb();
Michel Dänzer72a99872014-07-31 18:43:49 +0900177 /* If we are emitting the HDP flush via MMIO, we need to do it after
178 * all CPU writes to VRAM finished.
179 */
Michel Dänzer1538a9e2014-08-18 17:34:55 +0900180 if (hdp_flush && rdev->asic->mmio_hdp_flush)
Michel Dänzer72a99872014-07-31 18:43:49 +0900181 rdev->asic->mmio_hdp_flush(rdev);
Alex Deucherf93bdef2013-01-29 14:10:56 -0500182 radeon_ring_set_wptr(rdev, ring);
Matthew Garrett91700f32010-04-30 15:24:17 -0400183}
184
Alex Deucher75923282012-07-17 14:02:38 -0400185/**
186 * radeon_ring_unlock_commit - tell the GPU to execute the new
187 * commands on the ring buffer and unlock it
188 *
189 * @rdev: radeon_device pointer
190 * @ring: radeon_ring structure holding ring information
Michel Dänzer1538a9e2014-08-18 17:34:55 +0900191 * @hdp_flush: Whether or not to perform an HDP cache flush
Alex Deucher75923282012-07-17 14:02:38 -0400192 *
193 * Call radeon_ring_commit() then unlock the ring (all asics).
194 */
Michel Dänzer1538a9e2014-08-18 17:34:55 +0900195void radeon_ring_unlock_commit(struct radeon_device *rdev, struct radeon_ring *ring,
196 bool hdp_flush)
Matthew Garrett91700f32010-04-30 15:24:17 -0400197{
Michel Dänzer1538a9e2014-08-18 17:34:55 +0900198 radeon_ring_commit(rdev, ring, hdp_flush);
Christian Königd6999bc2012-05-09 15:34:45 +0200199 mutex_unlock(&rdev->ring_lock);
200}
201
Alex Deucher75923282012-07-17 14:02:38 -0400202/**
203 * radeon_ring_undo - reset the wptr
204 *
205 * @ring: radeon_ring structure holding ring information
206 *
Paul Bolle501f9d4c2012-11-20 22:31:06 +0100207 * Reset the driver's copy of the wptr (all asics).
Alex Deucher75923282012-07-17 14:02:38 -0400208 */
Christian Königd6999bc2012-05-09 15:34:45 +0200209void radeon_ring_undo(struct radeon_ring *ring)
210{
211 ring->wptr = ring->wptr_old;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200212}
213
Alex Deucher75923282012-07-17 14:02:38 -0400214/**
215 * radeon_ring_unlock_undo - reset the wptr and unlock the ring
216 *
217 * @ring: radeon_ring structure holding ring information
218 *
219 * Call radeon_ring_undo() then unlock the ring (all asics).
220 */
Christian Könige32eb502011-10-23 12:56:27 +0200221void radeon_ring_unlock_undo(struct radeon_device *rdev, struct radeon_ring *ring)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200222{
Christian Königd6999bc2012-05-09 15:34:45 +0200223 radeon_ring_undo(ring);
224 mutex_unlock(&rdev->ring_lock);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200225}
226
Alex Deucher75923282012-07-17 14:02:38 -0400227/**
Paul Bolle501f9d4c2012-11-20 22:31:06 +0100228 * radeon_ring_lockup_update - update lockup variables
Alex Deucher75923282012-07-17 14:02:38 -0400229 *
230 * @ring: radeon_ring structure holding ring information
231 *
232 * Update the last rptr value and timestamp (all asics).
233 */
Christian Königff212f22014-02-18 14:52:33 +0100234void radeon_ring_lockup_update(struct radeon_device *rdev,
235 struct radeon_ring *ring)
Christian König069211e2012-05-02 15:11:20 +0200236{
Christian Königaee4aa72014-02-18 15:24:06 +0100237 atomic_set(&ring->last_rptr, radeon_ring_get_rptr(rdev, ring));
238 atomic64_set(&ring->last_activity, jiffies_64);
Christian König069211e2012-05-02 15:11:20 +0200239}
240
241/**
242 * radeon_ring_test_lockup() - check if ring is lockedup by recording information
243 * @rdev: radeon device structure
244 * @ring: radeon_ring structure holding ring information
245 *
Christian König2d2fe3f2014-02-18 12:37:50 +0100246 */
Christian König069211e2012-05-02 15:11:20 +0200247bool radeon_ring_test_lockup(struct radeon_device *rdev, struct radeon_ring *ring)
248{
Christian Königff212f22014-02-18 14:52:33 +0100249 uint32_t rptr = radeon_ring_get_rptr(rdev, ring);
Christian Königaee4aa72014-02-18 15:24:06 +0100250 uint64_t last = atomic64_read(&ring->last_activity);
251 uint64_t elapsed;
Christian König069211e2012-05-02 15:11:20 +0200252
Christian Königaee4aa72014-02-18 15:24:06 +0100253 if (rptr != atomic_read(&ring->last_rptr)) {
254 /* ring is still working, no lockup */
Christian Königff212f22014-02-18 14:52:33 +0100255 radeon_ring_lockup_update(rdev, ring);
Christian König069211e2012-05-02 15:11:20 +0200256 return false;
257 }
Christian Königaee4aa72014-02-18 15:24:06 +0100258
259 elapsed = jiffies_to_msecs(jiffies_64 - last);
Christian König3368ff02012-05-02 15:11:21 +0200260 if (radeon_lockup_timeout && elapsed >= radeon_lockup_timeout) {
Christian Königaee4aa72014-02-18 15:24:06 +0100261 dev_err(rdev->dev, "ring %d stalled for more than %llumsec\n",
262 ring->idx, elapsed);
Christian König069211e2012-05-02 15:11:20 +0200263 return true;
264 }
265 /* give a chance to the GPU ... */
266 return false;
267}
268
Christian König55d7c222012-07-09 11:52:44 +0200269/**
270 * radeon_ring_backup - Back up the content of a ring
271 *
272 * @rdev: radeon_device pointer
273 * @ring: the ring we want to back up
274 *
275 * Saves all unprocessed commits from a ring, returns the number of dwords saved.
276 */
277unsigned radeon_ring_backup(struct radeon_device *rdev, struct radeon_ring *ring,
278 uint32_t **data)
279{
280 unsigned size, ptr, i;
Christian König55d7c222012-07-09 11:52:44 +0200281
282 /* just in case lock the ring */
283 mutex_lock(&rdev->ring_lock);
284 *data = NULL;
285
Alex Deucher89d35802012-07-17 14:02:31 -0400286 if (ring->ring_obj == NULL) {
Christian König55d7c222012-07-09 11:52:44 +0200287 mutex_unlock(&rdev->ring_lock);
288 return 0;
289 }
290
291 /* it doesn't make sense to save anything if all fences are signaled */
Alex Deucher8b25ed32012-07-17 14:02:30 -0400292 if (!radeon_fence_count_emitted(rdev, ring->idx)) {
Christian König55d7c222012-07-09 11:52:44 +0200293 mutex_unlock(&rdev->ring_lock);
294 return 0;
295 }
296
297 /* calculate the number of dw on the ring */
Alex Deucher89d35802012-07-17 14:02:31 -0400298 if (ring->rptr_save_reg)
299 ptr = RREG32(ring->rptr_save_reg);
300 else if (rdev->wb.enabled)
301 ptr = le32_to_cpu(*ring->next_rptr_cpu_addr);
302 else {
303 /* no way to read back the next rptr */
304 mutex_unlock(&rdev->ring_lock);
305 return 0;
306 }
307
Christian König55d7c222012-07-09 11:52:44 +0200308 size = ring->wptr + (ring->ring_size / 4);
309 size -= ptr;
310 size &= ring->ptr_mask;
311 if (size == 0) {
312 mutex_unlock(&rdev->ring_lock);
313 return 0;
314 }
315
316 /* and then save the content of the ring */
Michel Dänzere5a5fd42014-10-20 18:40:54 +0900317 *data = drm_malloc_ab(size, sizeof(uint32_t));
Dan Carpenter1e179d42012-07-20 14:17:00 +0300318 if (!*data) {
319 mutex_unlock(&rdev->ring_lock);
320 return 0;
321 }
Christian König55d7c222012-07-09 11:52:44 +0200322 for (i = 0; i < size; ++i) {
323 (*data)[i] = ring->ring[ptr++];
324 ptr &= ring->ptr_mask;
325 }
326
327 mutex_unlock(&rdev->ring_lock);
328 return size;
329}
330
331/**
332 * radeon_ring_restore - append saved commands to the ring again
333 *
334 * @rdev: radeon_device pointer
335 * @ring: ring to append commands to
336 * @size: number of dwords we want to write
337 * @data: saved commands
338 *
339 * Allocates space on the ring and restore the previously saved commands.
340 */
341int radeon_ring_restore(struct radeon_device *rdev, struct radeon_ring *ring,
342 unsigned size, uint32_t *data)
343{
344 int i, r;
345
346 if (!size || !data)
347 return 0;
348
349 /* restore the saved ring content */
350 r = radeon_ring_lock(rdev, ring, size);
351 if (r)
352 return r;
353
354 for (i = 0; i < size; ++i) {
355 radeon_ring_write(ring, data[i]);
356 }
357
Michel Dänzer1538a9e2014-08-18 17:34:55 +0900358 radeon_ring_unlock_commit(rdev, ring, false);
Michel Dänzere5a5fd42014-10-20 18:40:54 +0900359 drm_free_large(data);
Christian König55d7c222012-07-09 11:52:44 +0200360 return 0;
361}
362
Alex Deucher75923282012-07-17 14:02:38 -0400363/**
364 * radeon_ring_init - init driver ring struct.
365 *
366 * @rdev: radeon_device pointer
367 * @ring: radeon_ring structure holding ring information
368 * @ring_size: size of the ring
369 * @rptr_offs: offset of the rptr writeback location in the WB buffer
Alex Deucher75923282012-07-17 14:02:38 -0400370 * @nop: nop packet for this ring
371 *
372 * Initialize the driver information for the selected ring (all asics).
373 * Returns 0 on success, error on failure.
374 */
Christian Könige32eb502011-10-23 12:56:27 +0200375int radeon_ring_init(struct radeon_device *rdev, struct radeon_ring *ring, unsigned ring_size,
Alex Deucherea31bf62013-12-09 19:44:30 -0500376 unsigned rptr_offs, u32 nop)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200377{
378 int r;
379
Christian Könige32eb502011-10-23 12:56:27 +0200380 ring->ring_size = ring_size;
381 ring->rptr_offs = rptr_offs;
Alex Deucher78c55602011-11-17 14:25:56 -0500382 ring->nop = nop;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200383 /* Allocate ring buffer */
Christian Könige32eb502011-10-23 12:56:27 +0200384 if (ring->ring_obj == NULL) {
385 r = radeon_bo_create(rdev, ring->ring_size, PAGE_SIZE, true,
Maarten Lankhorst831b6962014-09-18 14:11:56 +0200386 RADEON_GEM_DOMAIN_GTT, 0, NULL,
Alex Deucher40f5cf92012-05-10 18:33:13 -0400387 NULL, &ring->ring_obj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200388 if (r) {
Jerome Glisse4c788672009-11-20 14:29:23 +0100389 dev_err(rdev->dev, "(%d) ring create failed\n", r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200390 return r;
391 }
Christian Könige32eb502011-10-23 12:56:27 +0200392 r = radeon_bo_reserve(ring->ring_obj, false);
Jerome Glisse4c788672009-11-20 14:29:23 +0100393 if (unlikely(r != 0))
394 return r;
Christian Könige32eb502011-10-23 12:56:27 +0200395 r = radeon_bo_pin(ring->ring_obj, RADEON_GEM_DOMAIN_GTT,
396 &ring->gpu_addr);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200397 if (r) {
Christian Könige32eb502011-10-23 12:56:27 +0200398 radeon_bo_unreserve(ring->ring_obj);
Jerome Glisse4c788672009-11-20 14:29:23 +0100399 dev_err(rdev->dev, "(%d) ring pin failed\n", r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200400 return r;
401 }
Christian Könige32eb502011-10-23 12:56:27 +0200402 r = radeon_bo_kmap(ring->ring_obj,
403 (void **)&ring->ring);
404 radeon_bo_unreserve(ring->ring_obj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200405 if (r) {
Jerome Glisse4c788672009-11-20 14:29:23 +0100406 dev_err(rdev->dev, "(%d) ring map failed\n", r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200407 return r;
408 }
409 }
Christian Könige32eb502011-10-23 12:56:27 +0200410 ring->ptr_mask = (ring->ring_size / 4) - 1;
411 ring->ring_free_dw = ring->ring_size / 4;
Alex Deucher89d35802012-07-17 14:02:31 -0400412 if (rdev->wb.enabled) {
413 u32 index = RADEON_WB_RING0_NEXT_RPTR + (ring->idx * 4);
414 ring->next_rptr_gpu_addr = rdev->wb.gpu_addr + index;
415 ring->next_rptr_cpu_addr = &rdev->wb.wb[index/4];
416 }
Christian Königec1a6cc2012-05-02 15:11:11 +0200417 if (radeon_debugfs_ring_init(rdev, ring)) {
418 DRM_ERROR("Failed to register debugfs file for rings !\n");
419 }
Christian Königff212f22014-02-18 14:52:33 +0100420 radeon_ring_lockup_update(rdev, ring);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200421 return 0;
422}
423
Alex Deucher75923282012-07-17 14:02:38 -0400424/**
425 * radeon_ring_fini - tear down the driver ring struct.
426 *
427 * @rdev: radeon_device pointer
428 * @ring: radeon_ring structure holding ring information
429 *
430 * Tear down the driver information for the selected ring (all asics).
431 */
Christian Könige32eb502011-10-23 12:56:27 +0200432void radeon_ring_fini(struct radeon_device *rdev, struct radeon_ring *ring)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200433{
Jerome Glisse4c788672009-11-20 14:29:23 +0100434 int r;
Alex Deucherca2af922010-05-06 11:02:24 -0400435 struct radeon_bo *ring_obj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100436
Christian Königd6999bc2012-05-09 15:34:45 +0200437 mutex_lock(&rdev->ring_lock);
Christian Könige32eb502011-10-23 12:56:27 +0200438 ring_obj = ring->ring_obj;
Christian Königd6999bc2012-05-09 15:34:45 +0200439 ring->ready = false;
Christian Könige32eb502011-10-23 12:56:27 +0200440 ring->ring = NULL;
441 ring->ring_obj = NULL;
Christian Königd6999bc2012-05-09 15:34:45 +0200442 mutex_unlock(&rdev->ring_lock);
Alex Deucherca2af922010-05-06 11:02:24 -0400443
444 if (ring_obj) {
445 r = radeon_bo_reserve(ring_obj, false);
446 if (likely(r == 0)) {
447 radeon_bo_kunmap(ring_obj);
448 radeon_bo_unpin(ring_obj);
449 radeon_bo_unreserve(ring_obj);
450 }
451 radeon_bo_unref(&ring_obj);
452 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200453}
454
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200455/*
456 * Debugfs info
457 */
458#if defined(CONFIG_DEBUG_FS)
Christian Königaf9720f2011-10-24 17:08:44 +0200459
460static int radeon_debugfs_ring_info(struct seq_file *m, void *data)
461{
462 struct drm_info_node *node = (struct drm_info_node *) m->private;
463 struct drm_device *dev = node->minor->dev;
464 struct radeon_device *rdev = dev->dev_private;
465 int ridx = *(int*)node->info_ent->data;
466 struct radeon_ring *ring = &rdev->ring[ridx];
Christian Königdf893a22013-12-12 09:42:37 +0100467
468 uint32_t rptr, wptr, rptr_next;
Christian Königaf9720f2011-10-24 17:08:44 +0200469 unsigned count, i, j;
470
471 radeon_ring_free_size(rdev, ring);
472 count = (ring->ring_size / 4) - ring->ring_free_dw;
Christian Königdf893a22013-12-12 09:42:37 +0100473
474 wptr = radeon_ring_get_wptr(rdev, ring);
Alex Deucherea31bf62013-12-09 19:44:30 -0500475 seq_printf(m, "wptr: 0x%08x [%5d]\n",
476 wptr, wptr);
Christian Königdf893a22013-12-12 09:42:37 +0100477
478 rptr = radeon_ring_get_rptr(rdev, ring);
Alex Deucherea31bf62013-12-09 19:44:30 -0500479 seq_printf(m, "rptr: 0x%08x [%5d]\n",
480 rptr, rptr);
Christian Königdf893a22013-12-12 09:42:37 +0100481
Christian König45df6802012-07-06 16:22:55 +0200482 if (ring->rptr_save_reg) {
Christian Königdf893a22013-12-12 09:42:37 +0100483 rptr_next = RREG32(ring->rptr_save_reg);
484 seq_printf(m, "rptr next(0x%04x): 0x%08x [%5d]\n",
485 ring->rptr_save_reg, rptr_next, rptr_next);
486 } else
487 rptr_next = ~0;
488
489 seq_printf(m, "driver's copy of the wptr: 0x%08x [%5d]\n",
490 ring->wptr, ring->wptr);
Christian Königdf893a22013-12-12 09:42:37 +0100491 seq_printf(m, "last semaphore signal addr : 0x%016llx\n",
492 ring->last_semaphore_signal_addr);
493 seq_printf(m, "last semaphore wait addr : 0x%016llx\n",
494 ring->last_semaphore_wait_addr);
Christian Königaf9720f2011-10-24 17:08:44 +0200495 seq_printf(m, "%u free dwords in ring\n", ring->ring_free_dw);
496 seq_printf(m, "%u dwords in ring\n", count);
Christian Königdf893a22013-12-12 09:42:37 +0100497
Christian König1b01fc32015-03-23 11:32:59 +0100498 if (!ring->ring)
Christian Königdf893a22013-12-12 09:42:37 +0100499 return 0;
500
Jerome Glisse4d009192013-01-02 17:30:34 -0500501 /* print 8 dw before current rptr as often it's the last executed
502 * packet that is the root issue
503 */
Christian Königdf893a22013-12-12 09:42:37 +0100504 i = (rptr + ring->ptr_mask + 1 - 32) & ring->ptr_mask;
505 for (j = 0; j <= (count + 32); j++) {
506 seq_printf(m, "r[%5d]=0x%08x", i, ring->ring[i]);
507 if (rptr == i)
508 seq_puts(m, " *");
509 if (rptr_next == i)
510 seq_puts(m, " #");
511 seq_puts(m, "\n");
512 i = (i + 1) & ring->ptr_mask;
Christian Königaf9720f2011-10-24 17:08:44 +0200513 }
514 return 0;
515}
516
Christian Königf2ba57b2013-04-08 12:41:29 +0200517static int radeon_gfx_index = RADEON_RING_TYPE_GFX_INDEX;
518static int cayman_cp1_index = CAYMAN_RING_TYPE_CP1_INDEX;
519static int cayman_cp2_index = CAYMAN_RING_TYPE_CP2_INDEX;
520static int radeon_dma1_index = R600_RING_TYPE_DMA_INDEX;
521static int radeon_dma2_index = CAYMAN_RING_TYPE_DMA1_INDEX;
522static int r600_uvd_index = R600_RING_TYPE_UVD_INDEX;
Christian Königd93f7932013-05-23 12:10:04 +0200523static int si_vce1_index = TN_RING_TYPE_VCE1_INDEX;
524static int si_vce2_index = TN_RING_TYPE_VCE2_INDEX;
Christian Königaf9720f2011-10-24 17:08:44 +0200525
526static struct drm_info_list radeon_debugfs_ring_info_list[] = {
Christian Königf2ba57b2013-04-08 12:41:29 +0200527 {"radeon_ring_gfx", radeon_debugfs_ring_info, 0, &radeon_gfx_index},
528 {"radeon_ring_cp1", radeon_debugfs_ring_info, 0, &cayman_cp1_index},
529 {"radeon_ring_cp2", radeon_debugfs_ring_info, 0, &cayman_cp2_index},
530 {"radeon_ring_dma1", radeon_debugfs_ring_info, 0, &radeon_dma1_index},
531 {"radeon_ring_dma2", radeon_debugfs_ring_info, 0, &radeon_dma2_index},
532 {"radeon_ring_uvd", radeon_debugfs_ring_info, 0, &r600_uvd_index},
Christian Königd93f7932013-05-23 12:10:04 +0200533 {"radeon_ring_vce1", radeon_debugfs_ring_info, 0, &si_vce1_index},
534 {"radeon_ring_vce2", radeon_debugfs_ring_info, 0, &si_vce2_index},
Christian Königaf9720f2011-10-24 17:08:44 +0200535};
536
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200537#endif
538
Lauri Kasanen1109ca02012-08-31 13:43:50 -0400539static int radeon_debugfs_ring_init(struct radeon_device *rdev, struct radeon_ring *ring)
Christian Königaf9720f2011-10-24 17:08:44 +0200540{
541#if defined(CONFIG_DEBUG_FS)
Christian Königec1a6cc2012-05-02 15:11:11 +0200542 unsigned i;
543 for (i = 0; i < ARRAY_SIZE(radeon_debugfs_ring_info_list); ++i) {
544 struct drm_info_list *info = &radeon_debugfs_ring_info_list[i];
545 int ridx = *(int*)radeon_debugfs_ring_info_list[i].data;
546 unsigned r;
547
548 if (&rdev->ring[ridx] != ring)
549 continue;
550
551 r = radeon_debugfs_add_files(rdev, info, 1);
552 if (r)
553 return r;
554 }
Christian Königaf9720f2011-10-24 17:08:44 +0200555#endif
Christian Königec1a6cc2012-05-02 15:11:11 +0200556 return 0;
Christian Königaf9720f2011-10-24 17:08:44 +0200557}