blob: d96b13af5e9d742996f844ca84d48a8312966675 [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
27 */
28#include <linux/seq_file.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Jerome Glisse771fe6b2009-06-05 14:42:42 +020030#include "drmP.h"
31#include "radeon_drm.h"
32#include "radeon_reg.h"
33#include "radeon.h"
34#include "atom.h"
35
36int radeon_debugfs_ib_init(struct radeon_device *rdev);
37
Andi Kleence580fa2011-10-13 16:08:47 -070038u32 radeon_get_ib_value(struct radeon_cs_parser *p, int idx)
39{
40 struct radeon_cs_chunk *ibc = &p->chunks[p->chunk_ib_idx];
41 u32 pg_idx, pg_offset;
42 u32 idx_value = 0;
43 int new_page;
44
45 pg_idx = (idx * 4) / PAGE_SIZE;
46 pg_offset = (idx * 4) % PAGE_SIZE;
47
48 if (ibc->kpage_idx[0] == pg_idx)
49 return ibc->kpage[0][pg_offset/4];
50 if (ibc->kpage_idx[1] == pg_idx)
51 return ibc->kpage[1][pg_offset/4];
52
53 new_page = radeon_cs_update_pages(p, pg_idx);
54 if (new_page < 0) {
55 p->parser_error = new_page;
56 return 0;
57 }
58
59 idx_value = ibc->kpage[new_page][pg_offset/4];
60 return idx_value;
61}
62
Christian König7b1f2482011-09-23 15:11:23 +020063void radeon_ring_write(struct radeon_cp *cp, uint32_t v)
Andi Kleence580fa2011-10-13 16:08:47 -070064{
65#if DRM_DEBUG_CODE
Christian König7b1f2482011-09-23 15:11:23 +020066 if (cp->count_dw <= 0) {
Andi Kleence580fa2011-10-13 16:08:47 -070067 DRM_ERROR("radeon: writting more dword to ring than expected !\n");
68 }
69#endif
Christian König7b1f2482011-09-23 15:11:23 +020070 cp->ring[cp->wptr++] = v;
71 cp->wptr &= cp->ptr_mask;
72 cp->count_dw--;
73 cp->ring_free_dw--;
Andi Kleence580fa2011-10-13 16:08:47 -070074}
75
Jerome Glisse9f93ed32010-01-28 18:22:31 +010076void radeon_ib_bogus_cleanup(struct radeon_device *rdev)
77{
78 struct radeon_ib *ib, *n;
79
80 list_for_each_entry_safe(ib, n, &rdev->ib_pool.bogus_ib, list) {
81 list_del(&ib->list);
82 vfree(ib->ptr);
83 kfree(ib);
84 }
85}
86
87void radeon_ib_bogus_add(struct radeon_device *rdev, struct radeon_ib *ib)
88{
89 struct radeon_ib *bib;
90
91 bib = kmalloc(sizeof(*bib), GFP_KERNEL);
92 if (bib == NULL)
93 return;
94 bib->ptr = vmalloc(ib->length_dw * 4);
95 if (bib->ptr == NULL) {
96 kfree(bib);
97 return;
98 }
99 memcpy(bib->ptr, ib->ptr, ib->length_dw * 4);
100 bib->length_dw = ib->length_dw;
101 mutex_lock(&rdev->ib_pool.mutex);
102 list_add_tail(&bib->list, &rdev->ib_pool.bogus_ib);
103 mutex_unlock(&rdev->ib_pool.mutex);
104}
105
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200106/*
107 * IB.
108 */
Christian König7b1f2482011-09-23 15:11:23 +0200109int radeon_ib_get(struct radeon_device *rdev, int ring, struct radeon_ib **ib)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200110{
111 struct radeon_fence *fence;
112 struct radeon_ib *nib;
Jerome Glisse91cb91b2010-02-15 21:36:13 +0100113 int r = 0, i, c;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200114
115 *ib = NULL;
Christian König7b1f2482011-09-23 15:11:23 +0200116 r = radeon_fence_create(rdev, &fence, ring);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200117 if (r) {
Jerome Glisse91cb91b2010-02-15 21:36:13 +0100118 dev_err(rdev->dev, "failed to create fence for new IB\n");
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200119 return r;
120 }
121 mutex_lock(&rdev->ib_pool.mutex);
Jerome Glisse91cb91b2010-02-15 21:36:13 +0100122 for (i = rdev->ib_pool.head_id, c = 0, nib = NULL; c < RADEON_IB_POOL_SIZE; c++, i++) {
123 i &= (RADEON_IB_POOL_SIZE - 1);
124 if (rdev->ib_pool.ibs[i].free) {
125 nib = &rdev->ib_pool.ibs[i];
126 break;
127 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200128 }
Jerome Glisse91cb91b2010-02-15 21:36:13 +0100129 if (nib == NULL) {
130 /* This should never happen, it means we allocated all
131 * IB and haven't scheduled one yet, return EBUSY to
132 * userspace hoping that on ioctl recall we get better
133 * luck
134 */
135 dev_err(rdev->dev, "no free indirect buffer !\n");
Dave Airlieecb114a2009-09-15 11:12:56 +1000136 mutex_unlock(&rdev->ib_pool.mutex);
Jerome Glisse91cb91b2010-02-15 21:36:13 +0100137 radeon_fence_unref(&fence);
138 return -EBUSY;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200139 }
Jerome Glisse91cb91b2010-02-15 21:36:13 +0100140 rdev->ib_pool.head_id = (nib->idx + 1) & (RADEON_IB_POOL_SIZE - 1);
141 nib->free = false;
142 if (nib->fence) {
Dave Airlieecb114a2009-09-15 11:12:56 +1000143 mutex_unlock(&rdev->ib_pool.mutex);
Jerome Glisse91cb91b2010-02-15 21:36:13 +0100144 r = radeon_fence_wait(nib->fence, false);
145 if (r) {
146 dev_err(rdev->dev, "error waiting fence of IB(%u:0x%016lX:%u)\n",
147 nib->idx, (unsigned long)nib->gpu_addr, nib->length_dw);
148 mutex_lock(&rdev->ib_pool.mutex);
149 nib->free = true;
150 mutex_unlock(&rdev->ib_pool.mutex);
151 radeon_fence_unref(&fence);
152 return r;
153 }
154 mutex_lock(&rdev->ib_pool.mutex);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200155 }
156 radeon_fence_unref(&nib->fence);
Jerome Glisse91cb91b2010-02-15 21:36:13 +0100157 nib->fence = fence;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200158 nib->length_dw = 0;
Dave Airlieecb114a2009-09-15 11:12:56 +1000159 mutex_unlock(&rdev->ib_pool.mutex);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200160 *ib = nib;
Jerome Glisse91cb91b2010-02-15 21:36:13 +0100161 return 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200162}
163
164void radeon_ib_free(struct radeon_device *rdev, struct radeon_ib **ib)
165{
166 struct radeon_ib *tmp = *ib;
167
168 *ib = NULL;
169 if (tmp == NULL) {
170 return;
171 }
Christian König851a6bd2011-10-24 15:05:29 +0200172 if (!tmp->fence->emitted)
Jerome Glisse7d404c72010-02-18 13:13:29 +0000173 radeon_fence_unref(&tmp->fence);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200174 mutex_lock(&rdev->ib_pool.mutex);
Jerome Glisse91cb91b2010-02-15 21:36:13 +0100175 tmp->free = true;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200176 mutex_unlock(&rdev->ib_pool.mutex);
177}
178
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200179int radeon_ib_schedule(struct radeon_device *rdev, struct radeon_ib *ib)
180{
Christian Königbf852792011-10-13 13:19:22 +0200181 struct radeon_cp *cp = &rdev->cp[ib->fence->ring];
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200182 int r = 0;
183
Christian König7b1f2482011-09-23 15:11:23 +0200184 if (!ib->length_dw || !cp->ready) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200185 /* TODO: Nothings in the ib we should report. */
Jerome Glisse91cb91b2010-02-15 21:36:13 +0100186 DRM_ERROR("radeon: couldn't schedule IB(%u).\n", ib->idx);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200187 return -EINVAL;
188 }
Dave Airlieecb114a2009-09-15 11:12:56 +1000189
Dave Airlie6cdf6582009-06-29 18:29:13 +1000190 /* 64 dwords should be enough for fence too */
Christian König7b1f2482011-09-23 15:11:23 +0200191 r = radeon_ring_lock(rdev, cp, 64);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200192 if (r) {
Paul Bolleec4f2ac2011-01-28 23:32:04 +0100193 DRM_ERROR("radeon: scheduling IB failed (%d).\n", r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200194 return r;
195 }
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000196 radeon_ring_ib_execute(rdev, ib);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200197 radeon_fence_emit(rdev, ib->fence);
Dave Airlieecb114a2009-09-15 11:12:56 +1000198 mutex_lock(&rdev->ib_pool.mutex);
Jerome Glisse91cb91b2010-02-15 21:36:13 +0100199 /* once scheduled IB is considered free and protected by the fence */
200 ib->free = true;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200201 mutex_unlock(&rdev->ib_pool.mutex);
Christian König7b1f2482011-09-23 15:11:23 +0200202 radeon_ring_unlock_commit(rdev, cp);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200203 return 0;
204}
205
206int radeon_ib_pool_init(struct radeon_device *rdev)
207{
208 void *ptr;
209 uint64_t gpu_addr;
210 int i;
211 int r = 0;
212
Jerome Glisse9f022dd2009-09-11 15:35:22 +0200213 if (rdev->ib_pool.robj)
214 return 0;
Jerome Glisse9f93ed32010-01-28 18:22:31 +0100215 INIT_LIST_HEAD(&rdev->ib_pool.bogus_ib);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200216 /* Allocate 1M object buffer */
Daniel Vetter441921d2011-02-18 17:59:16 +0100217 r = radeon_bo_create(rdev, RADEON_IB_POOL_SIZE*64*1024,
Alex Deucher268b2512010-11-17 19:00:26 -0500218 PAGE_SIZE, true, RADEON_GEM_DOMAIN_GTT,
219 &rdev->ib_pool.robj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200220 if (r) {
221 DRM_ERROR("radeon: failed to ib pool (%d).\n", r);
222 return r;
223 }
Jerome Glisse4c788672009-11-20 14:29:23 +0100224 r = radeon_bo_reserve(rdev->ib_pool.robj, false);
225 if (unlikely(r != 0))
226 return r;
227 r = radeon_bo_pin(rdev->ib_pool.robj, RADEON_GEM_DOMAIN_GTT, &gpu_addr);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200228 if (r) {
Jerome Glisse4c788672009-11-20 14:29:23 +0100229 radeon_bo_unreserve(rdev->ib_pool.robj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200230 DRM_ERROR("radeon: failed to pin ib pool (%d).\n", r);
231 return r;
232 }
Jerome Glisse4c788672009-11-20 14:29:23 +0100233 r = radeon_bo_kmap(rdev->ib_pool.robj, &ptr);
234 radeon_bo_unreserve(rdev->ib_pool.robj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200235 if (r) {
Paul Bolle205a44a2011-03-16 21:36:32 +0100236 DRM_ERROR("radeon: failed to map ib pool (%d).\n", r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200237 return r;
238 }
239 for (i = 0; i < RADEON_IB_POOL_SIZE; i++) {
240 unsigned offset;
241
242 offset = i * 64 * 1024;
243 rdev->ib_pool.ibs[i].gpu_addr = gpu_addr + offset;
244 rdev->ib_pool.ibs[i].ptr = ptr + offset;
245 rdev->ib_pool.ibs[i].idx = i;
246 rdev->ib_pool.ibs[i].length_dw = 0;
Jerome Glisse91cb91b2010-02-15 21:36:13 +0100247 rdev->ib_pool.ibs[i].free = true;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200248 }
Jerome Glisse91cb91b2010-02-15 21:36:13 +0100249 rdev->ib_pool.head_id = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200250 rdev->ib_pool.ready = true;
251 DRM_INFO("radeon: ib pool ready.\n");
252 if (radeon_debugfs_ib_init(rdev)) {
253 DRM_ERROR("Failed to register debugfs file for IB !\n");
254 }
255 return r;
256}
257
258void radeon_ib_pool_fini(struct radeon_device *rdev)
259{
Jerome Glisse4c788672009-11-20 14:29:23 +0100260 int r;
Alex Deucherca2af922010-05-06 11:02:24 -0400261 struct radeon_bo *robj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100262
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200263 if (!rdev->ib_pool.ready) {
264 return;
265 }
266 mutex_lock(&rdev->ib_pool.mutex);
Jerome Glisse9f93ed32010-01-28 18:22:31 +0100267 radeon_ib_bogus_cleanup(rdev);
Alex Deucherca2af922010-05-06 11:02:24 -0400268 robj = rdev->ib_pool.robj;
269 rdev->ib_pool.robj = NULL;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200270 mutex_unlock(&rdev->ib_pool.mutex);
Alex Deucherca2af922010-05-06 11:02:24 -0400271
272 if (robj) {
273 r = radeon_bo_reserve(robj, false);
274 if (likely(r == 0)) {
275 radeon_bo_kunmap(robj);
276 radeon_bo_unpin(robj);
277 radeon_bo_unreserve(robj);
278 }
279 radeon_bo_unref(&robj);
280 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200281}
282
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200283
284/*
285 * Ring.
286 */
Christian Königbf852792011-10-13 13:19:22 +0200287int radeon_ring_index(struct radeon_device *rdev, struct radeon_cp *cp)
288{
289 /* r1xx-r5xx only has CP ring */
290 if (rdev->family < CHIP_R600)
291 return RADEON_RING_TYPE_GFX_INDEX;
292
293 if (rdev->family >= CHIP_CAYMAN) {
294 if (cp == &rdev->cp[CAYMAN_RING_TYPE_CP1_INDEX])
295 return CAYMAN_RING_TYPE_CP1_INDEX;
296 else if (cp == &rdev->cp[CAYMAN_RING_TYPE_CP2_INDEX])
297 return CAYMAN_RING_TYPE_CP2_INDEX;
298 }
299 return RADEON_RING_TYPE_GFX_INDEX;
300}
301
Christian König7b1f2482011-09-23 15:11:23 +0200302void radeon_ring_free_size(struct radeon_device *rdev, struct radeon_cp *cp)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200303{
Alex Deucher724c80e2010-08-27 18:25:25 -0400304 if (rdev->wb.enabled)
Christian König5596a9d2011-10-13 12:48:45 +0200305 cp->rptr = le32_to_cpu(rdev->wb.wb[cp->rptr_offs/4]);
306 else
307 cp->rptr = RREG32(cp->rptr_reg);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200308 /* This works because ring_size is a power of 2 */
Christian König7b1f2482011-09-23 15:11:23 +0200309 cp->ring_free_dw = (cp->rptr + (cp->ring_size / 4));
310 cp->ring_free_dw -= cp->wptr;
311 cp->ring_free_dw &= cp->ptr_mask;
312 if (!cp->ring_free_dw) {
313 cp->ring_free_dw = cp->ring_size / 4;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200314 }
315}
316
Christian König7b1f2482011-09-23 15:11:23 +0200317
318int radeon_ring_alloc(struct radeon_device *rdev, struct radeon_cp *cp, unsigned ndw)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200319{
320 int r;
321
322 /* Align requested size with padding so unlock_commit can
323 * pad safely */
Christian König7b1f2482011-09-23 15:11:23 +0200324 ndw = (ndw + cp->align_mask) & ~cp->align_mask;
325 while (ndw > (cp->ring_free_dw - 1)) {
326 radeon_ring_free_size(rdev, cp);
327 if (ndw < cp->ring_free_dw) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200328 break;
329 }
Christian Königbf852792011-10-13 13:19:22 +0200330 r = radeon_fence_wait_next(rdev, radeon_ring_index(rdev, cp));
Matthew Garrett91700f32010-04-30 15:24:17 -0400331 if (r)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200332 return r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200333 }
Christian König7b1f2482011-09-23 15:11:23 +0200334 cp->count_dw = ndw;
335 cp->wptr_old = cp->wptr;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200336 return 0;
337}
338
Christian König7b1f2482011-09-23 15:11:23 +0200339int radeon_ring_lock(struct radeon_device *rdev, struct radeon_cp *cp, unsigned ndw)
Matthew Garrett91700f32010-04-30 15:24:17 -0400340{
341 int r;
342
Christian König7b1f2482011-09-23 15:11:23 +0200343 mutex_lock(&cp->mutex);
344 r = radeon_ring_alloc(rdev, cp, ndw);
Matthew Garrett91700f32010-04-30 15:24:17 -0400345 if (r) {
Christian König7b1f2482011-09-23 15:11:23 +0200346 mutex_unlock(&cp->mutex);
Matthew Garrett91700f32010-04-30 15:24:17 -0400347 return r;
348 }
349 return 0;
350}
351
Christian König7b1f2482011-09-23 15:11:23 +0200352void radeon_ring_commit(struct radeon_device *rdev, struct radeon_cp *cp)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200353{
354 unsigned count_dw_pad;
355 unsigned i;
356
357 /* We pad to match fetch size */
Christian König7b1f2482011-09-23 15:11:23 +0200358 count_dw_pad = (cp->align_mask + 1) -
359 (cp->wptr & cp->align_mask);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200360 for (i = 0; i < count_dw_pad; i++) {
Christian König7b1f2482011-09-23 15:11:23 +0200361 radeon_ring_write(cp, 2 << 30);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200362 }
363 DRM_MEMORYBARRIER();
Christian König5596a9d2011-10-13 12:48:45 +0200364 WREG32(cp->wptr_reg, cp->wptr);
365 (void)RREG32(cp->wptr_reg);
Matthew Garrett91700f32010-04-30 15:24:17 -0400366}
367
Christian König7b1f2482011-09-23 15:11:23 +0200368void radeon_ring_unlock_commit(struct radeon_device *rdev, struct radeon_cp *cp)
Matthew Garrett91700f32010-04-30 15:24:17 -0400369{
Christian König7b1f2482011-09-23 15:11:23 +0200370 radeon_ring_commit(rdev, cp);
371 mutex_unlock(&cp->mutex);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200372}
373
Christian König7b1f2482011-09-23 15:11:23 +0200374void radeon_ring_unlock_undo(struct radeon_device *rdev, struct radeon_cp *cp)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200375{
Christian König7b1f2482011-09-23 15:11:23 +0200376 cp->wptr = cp->wptr_old;
377 mutex_unlock(&cp->mutex);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200378}
379
Christian König5596a9d2011-10-13 12:48:45 +0200380int radeon_ring_init(struct radeon_device *rdev, struct radeon_cp *cp, unsigned ring_size,
381 unsigned rptr_offs, unsigned rptr_reg, unsigned wptr_reg)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200382{
383 int r;
384
Christian König7b1f2482011-09-23 15:11:23 +0200385 cp->ring_size = ring_size;
Christian König5596a9d2011-10-13 12:48:45 +0200386 cp->rptr_offs = rptr_offs;
387 cp->rptr_reg = rptr_reg;
388 cp->wptr_reg = wptr_reg;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200389 /* Allocate ring buffer */
Christian König7b1f2482011-09-23 15:11:23 +0200390 if (cp->ring_obj == NULL) {
391 r = radeon_bo_create(rdev, cp->ring_size, PAGE_SIZE, true,
Jerome Glisse4c788672009-11-20 14:29:23 +0100392 RADEON_GEM_DOMAIN_GTT,
Christian König7b1f2482011-09-23 15:11:23 +0200393 &cp->ring_obj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200394 if (r) {
Jerome Glisse4c788672009-11-20 14:29:23 +0100395 dev_err(rdev->dev, "(%d) ring create failed\n", r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200396 return r;
397 }
Christian König7b1f2482011-09-23 15:11:23 +0200398 r = radeon_bo_reserve(cp->ring_obj, false);
Jerome Glisse4c788672009-11-20 14:29:23 +0100399 if (unlikely(r != 0))
400 return r;
Christian König7b1f2482011-09-23 15:11:23 +0200401 r = radeon_bo_pin(cp->ring_obj, RADEON_GEM_DOMAIN_GTT,
402 &cp->gpu_addr);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200403 if (r) {
Christian König7b1f2482011-09-23 15:11:23 +0200404 radeon_bo_unreserve(cp->ring_obj);
Jerome Glisse4c788672009-11-20 14:29:23 +0100405 dev_err(rdev->dev, "(%d) ring pin failed\n", r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200406 return r;
407 }
Christian König7b1f2482011-09-23 15:11:23 +0200408 r = radeon_bo_kmap(cp->ring_obj,
409 (void **)&cp->ring);
410 radeon_bo_unreserve(cp->ring_obj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200411 if (r) {
Jerome Glisse4c788672009-11-20 14:29:23 +0100412 dev_err(rdev->dev, "(%d) ring map failed\n", r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200413 return r;
414 }
415 }
Christian König7b1f2482011-09-23 15:11:23 +0200416 cp->ptr_mask = (cp->ring_size / 4) - 1;
417 cp->ring_free_dw = cp->ring_size / 4;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200418 return 0;
419}
420
Christian König7b1f2482011-09-23 15:11:23 +0200421void radeon_ring_fini(struct radeon_device *rdev, struct radeon_cp *cp)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200422{
Jerome Glisse4c788672009-11-20 14:29:23 +0100423 int r;
Alex Deucherca2af922010-05-06 11:02:24 -0400424 struct radeon_bo *ring_obj;
Jerome Glisse4c788672009-11-20 14:29:23 +0100425
Christian König7b1f2482011-09-23 15:11:23 +0200426 mutex_lock(&cp->mutex);
427 ring_obj = cp->ring_obj;
428 cp->ring = NULL;
429 cp->ring_obj = NULL;
430 mutex_unlock(&cp->mutex);
Alex Deucherca2af922010-05-06 11:02:24 -0400431
432 if (ring_obj) {
433 r = radeon_bo_reserve(ring_obj, false);
434 if (likely(r == 0)) {
435 radeon_bo_kunmap(ring_obj);
436 radeon_bo_unpin(ring_obj);
437 radeon_bo_unreserve(ring_obj);
438 }
439 radeon_bo_unref(&ring_obj);
440 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200441}
442
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200443/*
444 * Debugfs info
445 */
446#if defined(CONFIG_DEBUG_FS)
447static int radeon_debugfs_ib_info(struct seq_file *m, void *data)
448{
449 struct drm_info_node *node = (struct drm_info_node *) m->private;
450 struct radeon_ib *ib = node->info_ent->data;
451 unsigned i;
452
453 if (ib == NULL) {
454 return 0;
455 }
Jerome Glisse91cb91b2010-02-15 21:36:13 +0100456 seq_printf(m, "IB %04u\n", ib->idx);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200457 seq_printf(m, "IB fence %p\n", ib->fence);
458 seq_printf(m, "IB size %05u dwords\n", ib->length_dw);
459 for (i = 0; i < ib->length_dw; i++) {
460 seq_printf(m, "[%05u]=0x%08X\n", i, ib->ptr[i]);
461 }
462 return 0;
463}
464
Jerome Glisse9f93ed32010-01-28 18:22:31 +0100465static int radeon_debugfs_ib_bogus_info(struct seq_file *m, void *data)
466{
467 struct drm_info_node *node = (struct drm_info_node *) m->private;
468 struct radeon_device *rdev = node->info_ent->data;
469 struct radeon_ib *ib;
470 unsigned i;
471
472 mutex_lock(&rdev->ib_pool.mutex);
473 if (list_empty(&rdev->ib_pool.bogus_ib)) {
474 mutex_unlock(&rdev->ib_pool.mutex);
475 seq_printf(m, "no bogus IB recorded\n");
476 return 0;
477 }
478 ib = list_first_entry(&rdev->ib_pool.bogus_ib, struct radeon_ib, list);
479 list_del_init(&ib->list);
480 mutex_unlock(&rdev->ib_pool.mutex);
481 seq_printf(m, "IB size %05u dwords\n", ib->length_dw);
482 for (i = 0; i < ib->length_dw; i++) {
483 seq_printf(m, "[%05u]=0x%08X\n", i, ib->ptr[i]);
484 }
485 vfree(ib->ptr);
486 kfree(ib);
487 return 0;
488}
489
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200490static struct drm_info_list radeon_debugfs_ib_list[RADEON_IB_POOL_SIZE];
491static char radeon_debugfs_ib_names[RADEON_IB_POOL_SIZE][32];
Jerome Glisse9f93ed32010-01-28 18:22:31 +0100492
493static struct drm_info_list radeon_debugfs_ib_bogus_info_list[] = {
494 {"radeon_ib_bogus", radeon_debugfs_ib_bogus_info, 0, NULL},
495};
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200496#endif
497
498int radeon_debugfs_ib_init(struct radeon_device *rdev)
499{
500#if defined(CONFIG_DEBUG_FS)
501 unsigned i;
Jerome Glisse9f93ed32010-01-28 18:22:31 +0100502 int r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200503
Jerome Glisse9f93ed32010-01-28 18:22:31 +0100504 radeon_debugfs_ib_bogus_info_list[0].data = rdev;
505 r = radeon_debugfs_add_files(rdev, radeon_debugfs_ib_bogus_info_list, 1);
506 if (r)
507 return r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200508 for (i = 0; i < RADEON_IB_POOL_SIZE; i++) {
509 sprintf(radeon_debugfs_ib_names[i], "radeon_ib_%04u", i);
510 radeon_debugfs_ib_list[i].name = radeon_debugfs_ib_names[i];
511 radeon_debugfs_ib_list[i].show = &radeon_debugfs_ib_info;
512 radeon_debugfs_ib_list[i].driver_features = 0;
513 radeon_debugfs_ib_list[i].data = &rdev->ib_pool.ibs[i];
514 }
515 return radeon_debugfs_add_files(rdev, radeon_debugfs_ib_list,
516 RADEON_IB_POOL_SIZE);
517#else
518 return 0;
519#endif
520}