blob: b3d6942a2be9912ed4bd08c461afcb9be40e31f5 [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;
96
97 return 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020098}
99
100void radeon_ib_free(struct radeon_device *rdev, struct radeon_ib **ib)
101{
102 struct radeon_ib *tmp = *ib;
103
104 *ib = NULL;
105 if (tmp == NULL) {
106 return;
107 }
Jerome Glissec507f7e2012-05-09 15:34:58 +0200108 radeon_sa_bo_free(rdev, &tmp->sa_bo, tmp->fence);
109 radeon_fence_unref(&tmp->fence);
110 kfree(tmp);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200111}
112
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200113int radeon_ib_schedule(struct radeon_device *rdev, struct radeon_ib *ib)
114{
Christian Könige32eb502011-10-23 12:56:27 +0200115 struct radeon_ring *ring = &rdev->ring[ib->fence->ring];
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200116 int r = 0;
117
Christian Könige32eb502011-10-23 12:56:27 +0200118 if (!ib->length_dw || !ring->ready) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200119 /* TODO: Nothings in the ib we should report. */
Jerome Glissec507f7e2012-05-09 15:34:58 +0200120 dev_err(rdev->dev, "couldn't schedule ib\n");
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200121 return -EINVAL;
122 }
Dave Airlieecb114a2009-09-15 11:12:56 +1000123
Dave Airlie6cdf6582009-06-29 18:29:13 +1000124 /* 64 dwords should be enough for fence too */
Christian Könige32eb502011-10-23 12:56:27 +0200125 r = radeon_ring_lock(rdev, ring, 64);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200126 if (r) {
Jerome Glissec507f7e2012-05-09 15:34:58 +0200127 dev_err(rdev->dev, "scheduling IB failed (%d).\n", r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200128 return r;
129 }
Christian König4c87bc22011-10-19 19:02:21 +0200130 radeon_ring_ib_execute(rdev, ib->fence->ring, ib);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200131 radeon_fence_emit(rdev, ib->fence);
Christian Könige32eb502011-10-23 12:56:27 +0200132 radeon_ring_unlock_commit(rdev, ring);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200133 return 0;
134}
135
136int radeon_ib_pool_init(struct radeon_device *rdev)
137{
Jerome Glissec507f7e2012-05-09 15:34:58 +0200138 int r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200139
Jerome Glissec507f7e2012-05-09 15:34:58 +0200140 if (rdev->ib_pool_ready) {
Jerome Glisse9f022dd2009-09-11 15:35:22 +0200141 return 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200142 }
Jerome Glissec507f7e2012-05-09 15:34:58 +0200143 r = radeon_sa_bo_manager_init(rdev, &rdev->ring_tmp_bo,
Christian Königc3b7fe82012-05-09 15:34:56 +0200144 RADEON_IB_POOL_SIZE*64*1024,
145 RADEON_GEM_DOMAIN_GTT);
146 if (r) {
Christian Königc3b7fe82012-05-09 15:34:56 +0200147 return r;
148 }
Jerome Glissec507f7e2012-05-09 15:34:58 +0200149 rdev->ib_pool_ready = true;
150 if (radeon_debugfs_sa_init(rdev)) {
151 dev_err(rdev->dev, "failed to register debugfs file for SA\n");
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200152 }
Jerome Glisseb15ba512011-11-15 11:48:34 -0500153 return 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200154}
155
156void radeon_ib_pool_fini(struct radeon_device *rdev)
157{
Jerome Glissec507f7e2012-05-09 15:34:58 +0200158 if (rdev->ib_pool_ready) {
159 radeon_sa_bo_manager_fini(rdev, &rdev->ring_tmp_bo);
160 rdev->ib_pool_ready = false;
Alex Deucherca2af922010-05-06 11:02:24 -0400161 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200162}
163
Jerome Glisseb15ba512011-11-15 11:48:34 -0500164int radeon_ib_pool_start(struct radeon_device *rdev)
165{
Jerome Glissec507f7e2012-05-09 15:34:58 +0200166 return radeon_sa_bo_manager_start(rdev, &rdev->ring_tmp_bo);
Jerome Glisseb15ba512011-11-15 11:48:34 -0500167}
168
169int radeon_ib_pool_suspend(struct radeon_device *rdev)
170{
Jerome Glissec507f7e2012-05-09 15:34:58 +0200171 return radeon_sa_bo_manager_suspend(rdev, &rdev->ring_tmp_bo);
Jerome Glisseb15ba512011-11-15 11:48:34 -0500172}
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200173
Christian König7bd560e2012-05-02 15:11:12 +0200174int radeon_ib_ring_tests(struct radeon_device *rdev)
175{
176 unsigned i;
177 int r;
178
179 for (i = 0; i < RADEON_NUM_RINGS; ++i) {
180 struct radeon_ring *ring = &rdev->ring[i];
181
182 if (!ring->ready)
183 continue;
184
185 r = radeon_ib_test(rdev, i, ring);
186 if (r) {
187 ring->ready = false;
188
189 if (i == RADEON_RING_TYPE_GFX_INDEX) {
190 /* oh, oh, that's really bad */
191 DRM_ERROR("radeon: failed testing IB on GFX ring (%d).\n", r);
192 rdev->accel_working = false;
193 return r;
194
195 } else {
196 /* still not good, but we can live with it */
197 DRM_ERROR("radeon: failed testing IB on ring %d (%d).\n", i, r);
198 }
199 }
200 }
201 return 0;
202}
203
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200204/*
205 * Ring.
206 */
Jerome Glissec507f7e2012-05-09 15:34:58 +0200207int radeon_debugfs_ring_init(struct radeon_device *rdev, struct radeon_ring *ring);
208
209void radeon_ring_write(struct radeon_ring *ring, uint32_t v)
210{
211#if DRM_DEBUG_CODE
212 if (ring->count_dw <= 0) {
213 DRM_ERROR("radeon: writting more dword to ring than expected !\n");
214 }
215#endif
216 ring->ring[ring->wptr++] = v;
217 ring->wptr &= ring->ptr_mask;
218 ring->count_dw--;
219 ring->ring_free_dw--;
220}
221
Christian Könige32eb502011-10-23 12:56:27 +0200222int radeon_ring_index(struct radeon_device *rdev, struct radeon_ring *ring)
Christian Königbf852792011-10-13 13:19:22 +0200223{
224 /* r1xx-r5xx only has CP ring */
225 if (rdev->family < CHIP_R600)
226 return RADEON_RING_TYPE_GFX_INDEX;
227
228 if (rdev->family >= CHIP_CAYMAN) {
Christian Könige32eb502011-10-23 12:56:27 +0200229 if (ring == &rdev->ring[CAYMAN_RING_TYPE_CP1_INDEX])
Christian Königbf852792011-10-13 13:19:22 +0200230 return CAYMAN_RING_TYPE_CP1_INDEX;
Christian Könige32eb502011-10-23 12:56:27 +0200231 else if (ring == &rdev->ring[CAYMAN_RING_TYPE_CP2_INDEX])
Christian Königbf852792011-10-13 13:19:22 +0200232 return CAYMAN_RING_TYPE_CP2_INDEX;
233 }
234 return RADEON_RING_TYPE_GFX_INDEX;
235}
236
Christian Könige32eb502011-10-23 12:56:27 +0200237void radeon_ring_free_size(struct radeon_device *rdev, struct radeon_ring *ring)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200238{
Alex Deucher78c55602011-11-17 14:25:56 -0500239 u32 rptr;
240
Alex Deucher724c80e2010-08-27 18:25:25 -0400241 if (rdev->wb.enabled)
Alex Deucher78c55602011-11-17 14:25:56 -0500242 rptr = le32_to_cpu(rdev->wb.wb[ring->rptr_offs/4]);
Christian König5596a9d2011-10-13 12:48:45 +0200243 else
Alex Deucher78c55602011-11-17 14:25:56 -0500244 rptr = RREG32(ring->rptr_reg);
245 ring->rptr = (rptr & ring->ptr_reg_mask) >> ring->ptr_reg_shift;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200246 /* This works because ring_size is a power of 2 */
Christian Könige32eb502011-10-23 12:56:27 +0200247 ring->ring_free_dw = (ring->rptr + (ring->ring_size / 4));
248 ring->ring_free_dw -= ring->wptr;
249 ring->ring_free_dw &= ring->ptr_mask;
250 if (!ring->ring_free_dw) {
251 ring->ring_free_dw = ring->ring_size / 4;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200252 }
253}
254
Christian König7b1f2482011-09-23 15:11:23 +0200255
Christian Könige32eb502011-10-23 12:56:27 +0200256int radeon_ring_alloc(struct radeon_device *rdev, struct radeon_ring *ring, unsigned ndw)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200257{
258 int r;
259
260 /* Align requested size with padding so unlock_commit can
261 * pad safely */
Christian Könige32eb502011-10-23 12:56:27 +0200262 ndw = (ndw + ring->align_mask) & ~ring->align_mask;
263 while (ndw > (ring->ring_free_dw - 1)) {
264 radeon_ring_free_size(rdev, ring);
265 if (ndw < ring->ring_free_dw) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200266 break;
267 }
Christian König8a47cc92012-05-09 15:34:48 +0200268 r = radeon_fence_wait_next_locked(rdev, radeon_ring_index(rdev, ring));
Matthew Garrett91700f32010-04-30 15:24:17 -0400269 if (r)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200270 return r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200271 }
Christian Könige32eb502011-10-23 12:56:27 +0200272 ring->count_dw = ndw;
273 ring->wptr_old = ring->wptr;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200274 return 0;
275}
276
Christian Könige32eb502011-10-23 12:56:27 +0200277int radeon_ring_lock(struct radeon_device *rdev, struct radeon_ring *ring, unsigned ndw)
Matthew Garrett91700f32010-04-30 15:24:17 -0400278{
279 int r;
280
Christian Königd6999bc2012-05-09 15:34:45 +0200281 mutex_lock(&rdev->ring_lock);
Christian Könige32eb502011-10-23 12:56:27 +0200282 r = radeon_ring_alloc(rdev, ring, ndw);
Matthew Garrett91700f32010-04-30 15:24:17 -0400283 if (r) {
Christian Königd6999bc2012-05-09 15:34:45 +0200284 mutex_unlock(&rdev->ring_lock);
Matthew Garrett91700f32010-04-30 15:24:17 -0400285 return r;
286 }
287 return 0;
288}
289
Christian Könige32eb502011-10-23 12:56:27 +0200290void radeon_ring_commit(struct radeon_device *rdev, struct radeon_ring *ring)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200291{
292 unsigned count_dw_pad;
293 unsigned i;
294
295 /* We pad to match fetch size */
Christian Könige32eb502011-10-23 12:56:27 +0200296 count_dw_pad = (ring->align_mask + 1) -
297 (ring->wptr & ring->align_mask);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200298 for (i = 0; i < count_dw_pad; i++) {
Alex Deucher78c55602011-11-17 14:25:56 -0500299 radeon_ring_write(ring, ring->nop);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200300 }
301 DRM_MEMORYBARRIER();
Alex Deucher78c55602011-11-17 14:25:56 -0500302 WREG32(ring->wptr_reg, (ring->wptr << ring->ptr_reg_shift) & ring->ptr_reg_mask);
Christian Könige32eb502011-10-23 12:56:27 +0200303 (void)RREG32(ring->wptr_reg);
Matthew Garrett91700f32010-04-30 15:24:17 -0400304}
305
Christian Könige32eb502011-10-23 12:56:27 +0200306void radeon_ring_unlock_commit(struct radeon_device *rdev, struct radeon_ring *ring)
Matthew Garrett91700f32010-04-30 15:24:17 -0400307{
Christian Könige32eb502011-10-23 12:56:27 +0200308 radeon_ring_commit(rdev, ring);
Christian Königd6999bc2012-05-09 15:34:45 +0200309 mutex_unlock(&rdev->ring_lock);
310}
311
312void radeon_ring_undo(struct radeon_ring *ring)
313{
314 ring->wptr = ring->wptr_old;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200315}
316
Christian Könige32eb502011-10-23 12:56:27 +0200317void radeon_ring_unlock_undo(struct radeon_device *rdev, struct radeon_ring *ring)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200318{
Christian Königd6999bc2012-05-09 15:34:45 +0200319 radeon_ring_undo(ring);
320 mutex_unlock(&rdev->ring_lock);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200321}
322
Christian König7b9ef162012-05-02 15:11:23 +0200323void radeon_ring_force_activity(struct radeon_device *rdev, struct radeon_ring *ring)
324{
325 int r;
326
Christian König7b9ef162012-05-02 15:11:23 +0200327 radeon_ring_free_size(rdev, ring);
328 if (ring->rptr == ring->wptr) {
329 r = radeon_ring_alloc(rdev, ring, 1);
330 if (!r) {
331 radeon_ring_write(ring, ring->nop);
332 radeon_ring_commit(rdev, ring);
333 }
334 }
Christian König7b9ef162012-05-02 15:11:23 +0200335}
336
Christian König069211e2012-05-02 15:11:20 +0200337void radeon_ring_lockup_update(struct radeon_ring *ring)
338{
339 ring->last_rptr = ring->rptr;
340 ring->last_activity = jiffies;
341}
342
343/**
344 * radeon_ring_test_lockup() - check if ring is lockedup by recording information
345 * @rdev: radeon device structure
346 * @ring: radeon_ring structure holding ring information
347 *
348 * We don't need to initialize the lockup tracking information as we will either
349 * have CP rptr to a different value of jiffies wrap around which will force
350 * initialization of the lockup tracking informations.
351 *
352 * A possible false positivie is if we get call after while and last_cp_rptr ==
353 * the current CP rptr, even if it's unlikely it might happen. To avoid this
354 * if the elapsed time since last call is bigger than 2 second than we return
355 * false and update the tracking information. Due to this the caller must call
356 * radeon_ring_test_lockup several time in less than 2sec for lockup to be reported
357 * the fencing code should be cautious about that.
358 *
359 * Caller should write to the ring to force CP to do something so we don't get
360 * false positive when CP is just gived nothing to do.
361 *
362 **/
363bool radeon_ring_test_lockup(struct radeon_device *rdev, struct radeon_ring *ring)
364{
365 unsigned long cjiffies, elapsed;
366 uint32_t rptr;
367
368 cjiffies = jiffies;
369 if (!time_after(cjiffies, ring->last_activity)) {
370 /* likely a wrap around */
371 radeon_ring_lockup_update(ring);
372 return false;
373 }
374 rptr = RREG32(ring->rptr_reg);
375 ring->rptr = (rptr & ring->ptr_reg_mask) >> ring->ptr_reg_shift;
376 if (ring->rptr != ring->last_rptr) {
377 /* CP is still working no lockup */
378 radeon_ring_lockup_update(ring);
379 return false;
380 }
381 elapsed = jiffies_to_msecs(cjiffies - ring->last_activity);
Christian König3368ff02012-05-02 15:11:21 +0200382 if (radeon_lockup_timeout && elapsed >= radeon_lockup_timeout) {
Christian König069211e2012-05-02 15:11:20 +0200383 dev_err(rdev->dev, "GPU lockup CP stall for more than %lumsec\n", elapsed);
384 return true;
385 }
386 /* give a chance to the GPU ... */
387 return false;
388}
389
Christian Könige32eb502011-10-23 12:56:27 +0200390int radeon_ring_init(struct radeon_device *rdev, struct radeon_ring *ring, unsigned ring_size,
Alex Deucher78c55602011-11-17 14:25:56 -0500391 unsigned rptr_offs, unsigned rptr_reg, unsigned wptr_reg,
392 u32 ptr_reg_shift, u32 ptr_reg_mask, u32 nop)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200393{
394 int r;
395
Christian Könige32eb502011-10-23 12:56:27 +0200396 ring->ring_size = ring_size;
397 ring->rptr_offs = rptr_offs;
398 ring->rptr_reg = rptr_reg;
399 ring->wptr_reg = wptr_reg;
Alex Deucher78c55602011-11-17 14:25:56 -0500400 ring->ptr_reg_shift = ptr_reg_shift;
401 ring->ptr_reg_mask = ptr_reg_mask;
402 ring->nop = nop;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200403 /* Allocate ring buffer */
Christian Könige32eb502011-10-23 12:56:27 +0200404 if (ring->ring_obj == NULL) {
405 r = radeon_bo_create(rdev, ring->ring_size, PAGE_SIZE, true,
Jerome Glisse4c788672009-11-20 14:29:23 +0100406 RADEON_GEM_DOMAIN_GTT,
Christian Könige32eb502011-10-23 12:56:27 +0200407 &ring->ring_obj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200408 if (r) {
Jerome Glisse4c788672009-11-20 14:29:23 +0100409 dev_err(rdev->dev, "(%d) ring create failed\n", r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200410 return r;
411 }
Christian Könige32eb502011-10-23 12:56:27 +0200412 r = radeon_bo_reserve(ring->ring_obj, false);
Jerome Glisse4c788672009-11-20 14:29:23 +0100413 if (unlikely(r != 0))
414 return r;
Christian Könige32eb502011-10-23 12:56:27 +0200415 r = radeon_bo_pin(ring->ring_obj, RADEON_GEM_DOMAIN_GTT,
416 &ring->gpu_addr);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200417 if (r) {
Christian Könige32eb502011-10-23 12:56:27 +0200418 radeon_bo_unreserve(ring->ring_obj);
Jerome Glisse4c788672009-11-20 14:29:23 +0100419 dev_err(rdev->dev, "(%d) ring pin failed\n", r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200420 return r;
421 }
Christian Könige32eb502011-10-23 12:56:27 +0200422 r = radeon_bo_kmap(ring->ring_obj,
423 (void **)&ring->ring);
424 radeon_bo_unreserve(ring->ring_obj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200425 if (r) {
Jerome Glisse4c788672009-11-20 14:29:23 +0100426 dev_err(rdev->dev, "(%d) ring map failed\n", r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200427 return r;
428 }
429 }
Christian Könige32eb502011-10-23 12:56:27 +0200430 ring->ptr_mask = (ring->ring_size / 4) - 1;
431 ring->ring_free_dw = ring->ring_size / 4;
Christian Königec1a6cc2012-05-02 15:11:11 +0200432 if (radeon_debugfs_ring_init(rdev, ring)) {
433 DRM_ERROR("Failed to register debugfs file for rings !\n");
434 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200435 return 0;
436}
437
Christian Könige32eb502011-10-23 12:56:27 +0200438void radeon_ring_fini(struct radeon_device *rdev, struct radeon_ring *ring)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200439{
Jerome Glisse4c788672009-11-20 14:29:23 +0100440 int r;
Alex Deucherca2af922010-05-06 11:02:24 -0400441 struct radeon_bo *ring_obj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100442
Christian Königd6999bc2012-05-09 15:34:45 +0200443 mutex_lock(&rdev->ring_lock);
Christian Könige32eb502011-10-23 12:56:27 +0200444 ring_obj = ring->ring_obj;
Christian Königd6999bc2012-05-09 15:34:45 +0200445 ring->ready = false;
Christian Könige32eb502011-10-23 12:56:27 +0200446 ring->ring = NULL;
447 ring->ring_obj = NULL;
Christian Königd6999bc2012-05-09 15:34:45 +0200448 mutex_unlock(&rdev->ring_lock);
Alex Deucherca2af922010-05-06 11:02:24 -0400449
450 if (ring_obj) {
451 r = radeon_bo_reserve(ring_obj, false);
452 if (likely(r == 0)) {
453 radeon_bo_kunmap(ring_obj);
454 radeon_bo_unpin(ring_obj);
455 radeon_bo_unreserve(ring_obj);
456 }
457 radeon_bo_unref(&ring_obj);
458 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200459}
460
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200461/*
462 * Debugfs info
463 */
464#if defined(CONFIG_DEBUG_FS)
Christian Königaf9720f2011-10-24 17:08:44 +0200465
466static int radeon_debugfs_ring_info(struct seq_file *m, void *data)
467{
468 struct drm_info_node *node = (struct drm_info_node *) m->private;
469 struct drm_device *dev = node->minor->dev;
470 struct radeon_device *rdev = dev->dev_private;
471 int ridx = *(int*)node->info_ent->data;
472 struct radeon_ring *ring = &rdev->ring[ridx];
473 unsigned count, i, j;
474
475 radeon_ring_free_size(rdev, ring);
476 count = (ring->ring_size / 4) - ring->ring_free_dw;
477 seq_printf(m, "wptr(0x%04x): 0x%08x\n", ring->wptr_reg, RREG32(ring->wptr_reg));
478 seq_printf(m, "rptr(0x%04x): 0x%08x\n", ring->rptr_reg, RREG32(ring->rptr_reg));
479 seq_printf(m, "driver's copy of the wptr: 0x%08x\n", ring->wptr);
480 seq_printf(m, "driver's copy of the rptr: 0x%08x\n", ring->rptr);
481 seq_printf(m, "%u free dwords in ring\n", ring->ring_free_dw);
482 seq_printf(m, "%u dwords in ring\n", count);
483 i = ring->rptr;
484 for (j = 0; j <= count; j++) {
485 seq_printf(m, "r[%04d]=0x%08x\n", i, ring->ring[i]);
486 i = (i + 1) & ring->ptr_mask;
487 }
488 return 0;
489}
490
491static int radeon_ring_type_gfx_index = RADEON_RING_TYPE_GFX_INDEX;
492static int cayman_ring_type_cp1_index = CAYMAN_RING_TYPE_CP1_INDEX;
493static int cayman_ring_type_cp2_index = CAYMAN_RING_TYPE_CP2_INDEX;
494
495static struct drm_info_list radeon_debugfs_ring_info_list[] = {
496 {"radeon_ring_gfx", radeon_debugfs_ring_info, 0, &radeon_ring_type_gfx_index},
497 {"radeon_ring_cp1", radeon_debugfs_ring_info, 0, &cayman_ring_type_cp1_index},
498 {"radeon_ring_cp2", radeon_debugfs_ring_info, 0, &cayman_ring_type_cp2_index},
499};
500
Christian König711a9722012-05-09 15:34:51 +0200501static int radeon_debugfs_sa_info(struct seq_file *m, void *data)
502{
503 struct drm_info_node *node = (struct drm_info_node *) m->private;
504 struct drm_device *dev = node->minor->dev;
505 struct radeon_device *rdev = dev->dev_private;
506
Jerome Glissec507f7e2012-05-09 15:34:58 +0200507 radeon_sa_bo_dump_debug_info(&rdev->ring_tmp_bo, m);
Christian König711a9722012-05-09 15:34:51 +0200508
509 return 0;
510
511}
512
513static struct drm_info_list radeon_debugfs_sa_list[] = {
514 {"radeon_sa_info", &radeon_debugfs_sa_info, 0, NULL},
515};
516
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200517#endif
518
Christian Königec1a6cc2012-05-02 15:11:11 +0200519int radeon_debugfs_ring_init(struct radeon_device *rdev, struct radeon_ring *ring)
Christian Königaf9720f2011-10-24 17:08:44 +0200520{
521#if defined(CONFIG_DEBUG_FS)
Christian Königec1a6cc2012-05-02 15:11:11 +0200522 unsigned i;
523 for (i = 0; i < ARRAY_SIZE(radeon_debugfs_ring_info_list); ++i) {
524 struct drm_info_list *info = &radeon_debugfs_ring_info_list[i];
525 int ridx = *(int*)radeon_debugfs_ring_info_list[i].data;
526 unsigned r;
527
528 if (&rdev->ring[ridx] != ring)
529 continue;
530
531 r = radeon_debugfs_add_files(rdev, info, 1);
532 if (r)
533 return r;
534 }
Christian Königaf9720f2011-10-24 17:08:44 +0200535#endif
Christian Königec1a6cc2012-05-02 15:11:11 +0200536 return 0;
Christian Königaf9720f2011-10-24 17:08:44 +0200537}
538
Jerome Glissec507f7e2012-05-09 15:34:58 +0200539int radeon_debugfs_sa_init(struct radeon_device *rdev)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200540{
541#if defined(CONFIG_DEBUG_FS)
Jerome Glissec507f7e2012-05-09 15:34:58 +0200542 return radeon_debugfs_add_files(rdev, radeon_debugfs_sa_list, 1);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200543#else
544 return 0;
545#endif
546}