blob: af8e1ee1dc0156d0d02c24462405e9593ad2022c [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 */
29#include <linux/seq_file.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Jerome Glisse771fe6b2009-06-05 14:42:42 +020031#include "drmP.h"
32#include "radeon_drm.h"
33#include "radeon_reg.h"
34#include "radeon.h"
35#include "atom.h"
36
Jerome Glissec507f7e2012-05-09 15:34:58 +020037/*
38 * IB.
39 */
40int radeon_debugfs_sa_init(struct radeon_device *rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +020041
Andi Kleence580fa2011-10-13 16:08:47 -070042u32 radeon_get_ib_value(struct radeon_cs_parser *p, int idx)
43{
44 struct radeon_cs_chunk *ibc = &p->chunks[p->chunk_ib_idx];
45 u32 pg_idx, pg_offset;
46 u32 idx_value = 0;
47 int new_page;
48
49 pg_idx = (idx * 4) / PAGE_SIZE;
50 pg_offset = (idx * 4) % PAGE_SIZE;
51
52 if (ibc->kpage_idx[0] == pg_idx)
53 return ibc->kpage[0][pg_offset/4];
54 if (ibc->kpage_idx[1] == pg_idx)
55 return ibc->kpage[1][pg_offset/4];
56
57 new_page = radeon_cs_update_pages(p, pg_idx);
58 if (new_page < 0) {
59 p->parser_error = new_page;
60 return 0;
61 }
62
63 idx_value = ibc->kpage[new_page][pg_offset/4];
64 return idx_value;
65}
66
Jerome Glisse69e130a2011-12-21 12:13:46 -050067int radeon_ib_get(struct radeon_device *rdev, int ring,
68 struct radeon_ib **ib, unsigned size)
Jerome Glisse771fe6b2009-06-05 14:42:42 +020069{
Jerome Glissec507f7e2012-05-09 15:34:58 +020070 int r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020071
Jerome Glissec507f7e2012-05-09 15:34:58 +020072 *ib = kmalloc(sizeof(struct radeon_ib), GFP_KERNEL);
73 if (*ib == NULL) {
74 return -ENOMEM;
75 }
76 r = radeon_sa_bo_new(rdev, &rdev->ring_tmp_bo, &(*ib)->sa_bo, size, 256, true);
Jerome Glisse771fe6b2009-06-05 14:42:42 +020077 if (r) {
Jerome Glissec507f7e2012-05-09 15:34:58 +020078 dev_err(rdev->dev, "failed to get a new IB (%d)\n", r);
79 kfree(*ib);
80 *ib = NULL;
81 return r;
82 }
83 r = radeon_fence_create(rdev, &(*ib)->fence, ring);
84 if (r) {
85 dev_err(rdev->dev, "failed to create fence for new IB (%d)\n", r);
86 radeon_sa_bo_free(rdev, &(*ib)->sa_bo, NULL);
87 kfree(*ib);
88 *ib = NULL;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020089 return r;
90 }
Jerome Glisseb15ba512011-11-15 11:48:34 -050091
Jerome Glissec507f7e2012-05-09 15:34:58 +020092 (*ib)->ptr = radeon_sa_bo_cpu_addr((*ib)->sa_bo);
93 (*ib)->gpu_addr = radeon_sa_bo_gpu_addr((*ib)->sa_bo);
94 (*ib)->vm_id = 0;
95 (*ib)->is_const_ib = false;
Jerome Glisse68470ae2012-05-09 15:35:00 +020096 (*ib)->semaphore = NULL;
Jerome Glissec507f7e2012-05-09 15:34:58 +020097
98 return 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020099}
100
101void radeon_ib_free(struct radeon_device *rdev, struct radeon_ib **ib)
102{
103 struct radeon_ib *tmp = *ib;
104
105 *ib = NULL;
106 if (tmp == NULL) {
107 return;
108 }
Jerome Glisse68470ae2012-05-09 15:35:00 +0200109 radeon_semaphore_free(rdev, tmp->semaphore, tmp->fence);
Jerome Glissec507f7e2012-05-09 15:34:58 +0200110 radeon_sa_bo_free(rdev, &tmp->sa_bo, tmp->fence);
111 radeon_fence_unref(&tmp->fence);
112 kfree(tmp);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200113}
114
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200115int radeon_ib_schedule(struct radeon_device *rdev, struct radeon_ib *ib)
116{
Christian Könige32eb502011-10-23 12:56:27 +0200117 struct radeon_ring *ring = &rdev->ring[ib->fence->ring];
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200118 int r = 0;
119
Christian Könige32eb502011-10-23 12:56:27 +0200120 if (!ib->length_dw || !ring->ready) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200121 /* TODO: Nothings in the ib we should report. */
Jerome Glissec507f7e2012-05-09 15:34:58 +0200122 dev_err(rdev->dev, "couldn't schedule ib\n");
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200123 return -EINVAL;
124 }
Dave Airlieecb114a2009-09-15 11:12:56 +1000125
Dave Airlie6cdf6582009-06-29 18:29:13 +1000126 /* 64 dwords should be enough for fence too */
Christian Könige32eb502011-10-23 12:56:27 +0200127 r = radeon_ring_lock(rdev, ring, 64);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200128 if (r) {
Jerome Glissec507f7e2012-05-09 15:34:58 +0200129 dev_err(rdev->dev, "scheduling IB failed (%d).\n", r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200130 return r;
131 }
Christian König4c87bc22011-10-19 19:02:21 +0200132 radeon_ring_ib_execute(rdev, ib->fence->ring, ib);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200133 radeon_fence_emit(rdev, ib->fence);
Christian Könige32eb502011-10-23 12:56:27 +0200134 radeon_ring_unlock_commit(rdev, ring);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200135 return 0;
136}
137
138int radeon_ib_pool_init(struct radeon_device *rdev)
139{
Jerome Glissec507f7e2012-05-09 15:34:58 +0200140 int r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200141
Jerome Glissec507f7e2012-05-09 15:34:58 +0200142 if (rdev->ib_pool_ready) {
Jerome Glisse9f022dd2009-09-11 15:35:22 +0200143 return 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200144 }
Jerome Glissec507f7e2012-05-09 15:34:58 +0200145 r = radeon_sa_bo_manager_init(rdev, &rdev->ring_tmp_bo,
Christian Königc3b7fe82012-05-09 15:34:56 +0200146 RADEON_IB_POOL_SIZE*64*1024,
147 RADEON_GEM_DOMAIN_GTT);
148 if (r) {
Christian Königc3b7fe82012-05-09 15:34:56 +0200149 return r;
150 }
Jerome Glissec507f7e2012-05-09 15:34:58 +0200151 rdev->ib_pool_ready = true;
152 if (radeon_debugfs_sa_init(rdev)) {
153 dev_err(rdev->dev, "failed to register debugfs file for SA\n");
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200154 }
Jerome Glisseb15ba512011-11-15 11:48:34 -0500155 return 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200156}
157
158void radeon_ib_pool_fini(struct radeon_device *rdev)
159{
Jerome Glissec507f7e2012-05-09 15:34:58 +0200160 if (rdev->ib_pool_ready) {
161 radeon_sa_bo_manager_fini(rdev, &rdev->ring_tmp_bo);
162 rdev->ib_pool_ready = false;
Alex Deucherca2af922010-05-06 11:02:24 -0400163 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200164}
165
Jerome Glisseb15ba512011-11-15 11:48:34 -0500166int radeon_ib_pool_start(struct radeon_device *rdev)
167{
Jerome Glissec507f7e2012-05-09 15:34:58 +0200168 return radeon_sa_bo_manager_start(rdev, &rdev->ring_tmp_bo);
Jerome Glisseb15ba512011-11-15 11:48:34 -0500169}
170
171int radeon_ib_pool_suspend(struct radeon_device *rdev)
172{
Jerome Glissec507f7e2012-05-09 15:34:58 +0200173 return radeon_sa_bo_manager_suspend(rdev, &rdev->ring_tmp_bo);
Jerome Glisseb15ba512011-11-15 11:48:34 -0500174}
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200175
Christian König7bd560e2012-05-02 15:11:12 +0200176int radeon_ib_ring_tests(struct radeon_device *rdev)
177{
178 unsigned i;
179 int r;
180
181 for (i = 0; i < RADEON_NUM_RINGS; ++i) {
182 struct radeon_ring *ring = &rdev->ring[i];
183
184 if (!ring->ready)
185 continue;
186
187 r = radeon_ib_test(rdev, i, ring);
188 if (r) {
189 ring->ready = false;
190
191 if (i == RADEON_RING_TYPE_GFX_INDEX) {
192 /* oh, oh, that's really bad */
193 DRM_ERROR("radeon: failed testing IB on GFX ring (%d).\n", r);
194 rdev->accel_working = false;
195 return r;
196
197 } else {
198 /* still not good, but we can live with it */
199 DRM_ERROR("radeon: failed testing IB on ring %d (%d).\n", i, r);
200 }
201 }
202 }
203 return 0;
204}
205
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200206/*
207 * Ring.
208 */
Jerome Glissec507f7e2012-05-09 15:34:58 +0200209int radeon_debugfs_ring_init(struct radeon_device *rdev, struct radeon_ring *ring);
210
211void radeon_ring_write(struct radeon_ring *ring, uint32_t v)
212{
213#if DRM_DEBUG_CODE
214 if (ring->count_dw <= 0) {
215 DRM_ERROR("radeon: writting more dword to ring than expected !\n");
216 }
217#endif
218 ring->ring[ring->wptr++] = v;
219 ring->wptr &= ring->ptr_mask;
220 ring->count_dw--;
221 ring->ring_free_dw--;
222}
223
Christian Könige32eb502011-10-23 12:56:27 +0200224int radeon_ring_index(struct radeon_device *rdev, struct radeon_ring *ring)
Christian Königbf852792011-10-13 13:19:22 +0200225{
226 /* r1xx-r5xx only has CP ring */
227 if (rdev->family < CHIP_R600)
228 return RADEON_RING_TYPE_GFX_INDEX;
229
230 if (rdev->family >= CHIP_CAYMAN) {
Christian Könige32eb502011-10-23 12:56:27 +0200231 if (ring == &rdev->ring[CAYMAN_RING_TYPE_CP1_INDEX])
Christian Königbf852792011-10-13 13:19:22 +0200232 return CAYMAN_RING_TYPE_CP1_INDEX;
Christian Könige32eb502011-10-23 12:56:27 +0200233 else if (ring == &rdev->ring[CAYMAN_RING_TYPE_CP2_INDEX])
Christian Königbf852792011-10-13 13:19:22 +0200234 return CAYMAN_RING_TYPE_CP2_INDEX;
235 }
236 return RADEON_RING_TYPE_GFX_INDEX;
237}
238
Christian Könige32eb502011-10-23 12:56:27 +0200239void radeon_ring_free_size(struct radeon_device *rdev, struct radeon_ring *ring)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200240{
Alex Deucher78c55602011-11-17 14:25:56 -0500241 u32 rptr;
242
Alex Deucher724c80e2010-08-27 18:25:25 -0400243 if (rdev->wb.enabled)
Alex Deucher78c55602011-11-17 14:25:56 -0500244 rptr = le32_to_cpu(rdev->wb.wb[ring->rptr_offs/4]);
Christian König5596a9d2011-10-13 12:48:45 +0200245 else
Alex Deucher78c55602011-11-17 14:25:56 -0500246 rptr = RREG32(ring->rptr_reg);
247 ring->rptr = (rptr & ring->ptr_reg_mask) >> ring->ptr_reg_shift;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200248 /* This works because ring_size is a power of 2 */
Christian Könige32eb502011-10-23 12:56:27 +0200249 ring->ring_free_dw = (ring->rptr + (ring->ring_size / 4));
250 ring->ring_free_dw -= ring->wptr;
251 ring->ring_free_dw &= ring->ptr_mask;
252 if (!ring->ring_free_dw) {
253 ring->ring_free_dw = ring->ring_size / 4;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200254 }
255}
256
Christian König7b1f2482011-09-23 15:11:23 +0200257
Christian Könige32eb502011-10-23 12:56:27 +0200258int radeon_ring_alloc(struct radeon_device *rdev, struct radeon_ring *ring, unsigned ndw)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200259{
260 int r;
261
262 /* Align requested size with padding so unlock_commit can
263 * pad safely */
Christian Könige32eb502011-10-23 12:56:27 +0200264 ndw = (ndw + ring->align_mask) & ~ring->align_mask;
265 while (ndw > (ring->ring_free_dw - 1)) {
266 radeon_ring_free_size(rdev, ring);
267 if (ndw < ring->ring_free_dw) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200268 break;
269 }
Christian König8a47cc92012-05-09 15:34:48 +0200270 r = radeon_fence_wait_next_locked(rdev, radeon_ring_index(rdev, ring));
Matthew Garrett91700f32010-04-30 15:24:17 -0400271 if (r)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200272 return r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200273 }
Christian Könige32eb502011-10-23 12:56:27 +0200274 ring->count_dw = ndw;
275 ring->wptr_old = ring->wptr;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200276 return 0;
277}
278
Christian Könige32eb502011-10-23 12:56:27 +0200279int radeon_ring_lock(struct radeon_device *rdev, struct radeon_ring *ring, unsigned ndw)
Matthew Garrett91700f32010-04-30 15:24:17 -0400280{
281 int r;
282
Christian Königd6999bc2012-05-09 15:34:45 +0200283 mutex_lock(&rdev->ring_lock);
Christian Könige32eb502011-10-23 12:56:27 +0200284 r = radeon_ring_alloc(rdev, ring, ndw);
Matthew Garrett91700f32010-04-30 15:24:17 -0400285 if (r) {
Christian Königd6999bc2012-05-09 15:34:45 +0200286 mutex_unlock(&rdev->ring_lock);
Matthew Garrett91700f32010-04-30 15:24:17 -0400287 return r;
288 }
289 return 0;
290}
291
Christian Könige32eb502011-10-23 12:56:27 +0200292void radeon_ring_commit(struct radeon_device *rdev, struct radeon_ring *ring)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200293{
294 unsigned count_dw_pad;
295 unsigned i;
296
297 /* We pad to match fetch size */
Christian Könige32eb502011-10-23 12:56:27 +0200298 count_dw_pad = (ring->align_mask + 1) -
299 (ring->wptr & ring->align_mask);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200300 for (i = 0; i < count_dw_pad; i++) {
Alex Deucher78c55602011-11-17 14:25:56 -0500301 radeon_ring_write(ring, ring->nop);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200302 }
303 DRM_MEMORYBARRIER();
Alex Deucher78c55602011-11-17 14:25:56 -0500304 WREG32(ring->wptr_reg, (ring->wptr << ring->ptr_reg_shift) & ring->ptr_reg_mask);
Christian Könige32eb502011-10-23 12:56:27 +0200305 (void)RREG32(ring->wptr_reg);
Matthew Garrett91700f32010-04-30 15:24:17 -0400306}
307
Christian Könige32eb502011-10-23 12:56:27 +0200308void radeon_ring_unlock_commit(struct radeon_device *rdev, struct radeon_ring *ring)
Matthew Garrett91700f32010-04-30 15:24:17 -0400309{
Christian Könige32eb502011-10-23 12:56:27 +0200310 radeon_ring_commit(rdev, ring);
Christian Königd6999bc2012-05-09 15:34:45 +0200311 mutex_unlock(&rdev->ring_lock);
312}
313
314void radeon_ring_undo(struct radeon_ring *ring)
315{
316 ring->wptr = ring->wptr_old;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200317}
318
Christian Könige32eb502011-10-23 12:56:27 +0200319void radeon_ring_unlock_undo(struct radeon_device *rdev, struct radeon_ring *ring)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200320{
Christian Königd6999bc2012-05-09 15:34:45 +0200321 radeon_ring_undo(ring);
322 mutex_unlock(&rdev->ring_lock);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200323}
324
Christian König7b9ef162012-05-02 15:11:23 +0200325void radeon_ring_force_activity(struct radeon_device *rdev, struct radeon_ring *ring)
326{
327 int r;
328
Christian König7b9ef162012-05-02 15:11:23 +0200329 radeon_ring_free_size(rdev, ring);
330 if (ring->rptr == ring->wptr) {
331 r = radeon_ring_alloc(rdev, ring, 1);
332 if (!r) {
333 radeon_ring_write(ring, ring->nop);
334 radeon_ring_commit(rdev, ring);
335 }
336 }
Christian König7b9ef162012-05-02 15:11:23 +0200337}
338
Christian König069211e2012-05-02 15:11:20 +0200339void radeon_ring_lockup_update(struct radeon_ring *ring)
340{
341 ring->last_rptr = ring->rptr;
342 ring->last_activity = jiffies;
343}
344
345/**
346 * radeon_ring_test_lockup() - check if ring is lockedup by recording information
347 * @rdev: radeon device structure
348 * @ring: radeon_ring structure holding ring information
349 *
350 * We don't need to initialize the lockup tracking information as we will either
351 * have CP rptr to a different value of jiffies wrap around which will force
352 * initialization of the lockup tracking informations.
353 *
354 * A possible false positivie is if we get call after while and last_cp_rptr ==
355 * the current CP rptr, even if it's unlikely it might happen. To avoid this
356 * if the elapsed time since last call is bigger than 2 second than we return
357 * false and update the tracking information. Due to this the caller must call
358 * radeon_ring_test_lockup several time in less than 2sec for lockup to be reported
359 * the fencing code should be cautious about that.
360 *
361 * Caller should write to the ring to force CP to do something so we don't get
362 * false positive when CP is just gived nothing to do.
363 *
364 **/
365bool radeon_ring_test_lockup(struct radeon_device *rdev, struct radeon_ring *ring)
366{
367 unsigned long cjiffies, elapsed;
368 uint32_t rptr;
369
370 cjiffies = jiffies;
371 if (!time_after(cjiffies, ring->last_activity)) {
372 /* likely a wrap around */
373 radeon_ring_lockup_update(ring);
374 return false;
375 }
376 rptr = RREG32(ring->rptr_reg);
377 ring->rptr = (rptr & ring->ptr_reg_mask) >> ring->ptr_reg_shift;
378 if (ring->rptr != ring->last_rptr) {
379 /* CP is still working no lockup */
380 radeon_ring_lockup_update(ring);
381 return false;
382 }
383 elapsed = jiffies_to_msecs(cjiffies - ring->last_activity);
Christian König3368ff02012-05-02 15:11:21 +0200384 if (radeon_lockup_timeout && elapsed >= radeon_lockup_timeout) {
Christian König069211e2012-05-02 15:11:20 +0200385 dev_err(rdev->dev, "GPU lockup CP stall for more than %lumsec\n", elapsed);
386 return true;
387 }
388 /* give a chance to the GPU ... */
389 return false;
390}
391
Christian Könige32eb502011-10-23 12:56:27 +0200392int radeon_ring_init(struct radeon_device *rdev, struct radeon_ring *ring, unsigned ring_size,
Alex Deucher78c55602011-11-17 14:25:56 -0500393 unsigned rptr_offs, unsigned rptr_reg, unsigned wptr_reg,
394 u32 ptr_reg_shift, u32 ptr_reg_mask, u32 nop)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200395{
396 int r;
397
Christian Könige32eb502011-10-23 12:56:27 +0200398 ring->ring_size = ring_size;
399 ring->rptr_offs = rptr_offs;
400 ring->rptr_reg = rptr_reg;
401 ring->wptr_reg = wptr_reg;
Alex Deucher78c55602011-11-17 14:25:56 -0500402 ring->ptr_reg_shift = ptr_reg_shift;
403 ring->ptr_reg_mask = ptr_reg_mask;
404 ring->nop = nop;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200405 /* Allocate ring buffer */
Christian Könige32eb502011-10-23 12:56:27 +0200406 if (ring->ring_obj == NULL) {
407 r = radeon_bo_create(rdev, ring->ring_size, PAGE_SIZE, true,
Jerome Glisse4c788672009-11-20 14:29:23 +0100408 RADEON_GEM_DOMAIN_GTT,
Christian Könige32eb502011-10-23 12:56:27 +0200409 &ring->ring_obj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200410 if (r) {
Jerome Glisse4c788672009-11-20 14:29:23 +0100411 dev_err(rdev->dev, "(%d) ring create failed\n", r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200412 return r;
413 }
Christian Könige32eb502011-10-23 12:56:27 +0200414 r = radeon_bo_reserve(ring->ring_obj, false);
Jerome Glisse4c788672009-11-20 14:29:23 +0100415 if (unlikely(r != 0))
416 return r;
Christian Könige32eb502011-10-23 12:56:27 +0200417 r = radeon_bo_pin(ring->ring_obj, RADEON_GEM_DOMAIN_GTT,
418 &ring->gpu_addr);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200419 if (r) {
Christian Könige32eb502011-10-23 12:56:27 +0200420 radeon_bo_unreserve(ring->ring_obj);
Jerome Glisse4c788672009-11-20 14:29:23 +0100421 dev_err(rdev->dev, "(%d) ring pin failed\n", r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200422 return r;
423 }
Christian Könige32eb502011-10-23 12:56:27 +0200424 r = radeon_bo_kmap(ring->ring_obj,
425 (void **)&ring->ring);
426 radeon_bo_unreserve(ring->ring_obj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200427 if (r) {
Jerome Glisse4c788672009-11-20 14:29:23 +0100428 dev_err(rdev->dev, "(%d) ring map failed\n", r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200429 return r;
430 }
431 }
Christian Könige32eb502011-10-23 12:56:27 +0200432 ring->ptr_mask = (ring->ring_size / 4) - 1;
433 ring->ring_free_dw = ring->ring_size / 4;
Christian Königec1a6cc2012-05-02 15:11:11 +0200434 if (radeon_debugfs_ring_init(rdev, ring)) {
435 DRM_ERROR("Failed to register debugfs file for rings !\n");
436 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200437 return 0;
438}
439
Christian Könige32eb502011-10-23 12:56:27 +0200440void radeon_ring_fini(struct radeon_device *rdev, struct radeon_ring *ring)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200441{
Jerome Glisse4c788672009-11-20 14:29:23 +0100442 int r;
Alex Deucherca2af922010-05-06 11:02:24 -0400443 struct radeon_bo *ring_obj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100444
Christian Königd6999bc2012-05-09 15:34:45 +0200445 mutex_lock(&rdev->ring_lock);
Christian Könige32eb502011-10-23 12:56:27 +0200446 ring_obj = ring->ring_obj;
Christian Königd6999bc2012-05-09 15:34:45 +0200447 ring->ready = false;
Christian Könige32eb502011-10-23 12:56:27 +0200448 ring->ring = NULL;
449 ring->ring_obj = NULL;
Christian Königd6999bc2012-05-09 15:34:45 +0200450 mutex_unlock(&rdev->ring_lock);
Alex Deucherca2af922010-05-06 11:02:24 -0400451
452 if (ring_obj) {
453 r = radeon_bo_reserve(ring_obj, false);
454 if (likely(r == 0)) {
455 radeon_bo_kunmap(ring_obj);
456 radeon_bo_unpin(ring_obj);
457 radeon_bo_unreserve(ring_obj);
458 }
459 radeon_bo_unref(&ring_obj);
460 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200461}
462
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200463/*
464 * Debugfs info
465 */
466#if defined(CONFIG_DEBUG_FS)
Christian Königaf9720f2011-10-24 17:08:44 +0200467
468static int radeon_debugfs_ring_info(struct seq_file *m, void *data)
469{
470 struct drm_info_node *node = (struct drm_info_node *) m->private;
471 struct drm_device *dev = node->minor->dev;
472 struct radeon_device *rdev = dev->dev_private;
473 int ridx = *(int*)node->info_ent->data;
474 struct radeon_ring *ring = &rdev->ring[ridx];
475 unsigned count, i, j;
476
477 radeon_ring_free_size(rdev, ring);
478 count = (ring->ring_size / 4) - ring->ring_free_dw;
479 seq_printf(m, "wptr(0x%04x): 0x%08x\n", ring->wptr_reg, RREG32(ring->wptr_reg));
480 seq_printf(m, "rptr(0x%04x): 0x%08x\n", ring->rptr_reg, RREG32(ring->rptr_reg));
481 seq_printf(m, "driver's copy of the wptr: 0x%08x\n", ring->wptr);
482 seq_printf(m, "driver's copy of the rptr: 0x%08x\n", ring->rptr);
483 seq_printf(m, "%u free dwords in ring\n", ring->ring_free_dw);
484 seq_printf(m, "%u dwords in ring\n", count);
485 i = ring->rptr;
486 for (j = 0; j <= count; j++) {
487 seq_printf(m, "r[%04d]=0x%08x\n", i, ring->ring[i]);
488 i = (i + 1) & ring->ptr_mask;
489 }
490 return 0;
491}
492
493static int radeon_ring_type_gfx_index = RADEON_RING_TYPE_GFX_INDEX;
494static int cayman_ring_type_cp1_index = CAYMAN_RING_TYPE_CP1_INDEX;
495static int cayman_ring_type_cp2_index = CAYMAN_RING_TYPE_CP2_INDEX;
496
497static struct drm_info_list radeon_debugfs_ring_info_list[] = {
498 {"radeon_ring_gfx", radeon_debugfs_ring_info, 0, &radeon_ring_type_gfx_index},
499 {"radeon_ring_cp1", radeon_debugfs_ring_info, 0, &cayman_ring_type_cp1_index},
500 {"radeon_ring_cp2", radeon_debugfs_ring_info, 0, &cayman_ring_type_cp2_index},
501};
502
Christian König711a9722012-05-09 15:34:51 +0200503static int radeon_debugfs_sa_info(struct seq_file *m, void *data)
504{
505 struct drm_info_node *node = (struct drm_info_node *) m->private;
506 struct drm_device *dev = node->minor->dev;
507 struct radeon_device *rdev = dev->dev_private;
508
Jerome Glissec507f7e2012-05-09 15:34:58 +0200509 radeon_sa_bo_dump_debug_info(&rdev->ring_tmp_bo, m);
Christian König711a9722012-05-09 15:34:51 +0200510
511 return 0;
512
513}
514
515static struct drm_info_list radeon_debugfs_sa_list[] = {
516 {"radeon_sa_info", &radeon_debugfs_sa_info, 0, NULL},
517};
518
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200519#endif
520
Christian Königec1a6cc2012-05-02 15:11:11 +0200521int radeon_debugfs_ring_init(struct radeon_device *rdev, struct radeon_ring *ring)
Christian Königaf9720f2011-10-24 17:08:44 +0200522{
523#if defined(CONFIG_DEBUG_FS)
Christian Königec1a6cc2012-05-02 15:11:11 +0200524 unsigned i;
525 for (i = 0; i < ARRAY_SIZE(radeon_debugfs_ring_info_list); ++i) {
526 struct drm_info_list *info = &radeon_debugfs_ring_info_list[i];
527 int ridx = *(int*)radeon_debugfs_ring_info_list[i].data;
528 unsigned r;
529
530 if (&rdev->ring[ridx] != ring)
531 continue;
532
533 r = radeon_debugfs_add_files(rdev, info, 1);
534 if (r)
535 return r;
536 }
Christian Königaf9720f2011-10-24 17:08:44 +0200537#endif
Christian Königec1a6cc2012-05-02 15:11:11 +0200538 return 0;
Christian Königaf9720f2011-10-24 17:08:44 +0200539}
540
Jerome Glissec507f7e2012-05-09 15:34:58 +0200541int radeon_debugfs_sa_init(struct radeon_device *rdev)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200542{
543#if defined(CONFIG_DEBUG_FS)
Jerome Glissec507f7e2012-05-09 15:34:58 +0200544 return radeon_debugfs_add_files(rdev, radeon_debugfs_sa_list, 1);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200545#else
546 return 0;
547#endif
548}