blob: 694799f6fac1e7fd75c60d79398216d34e249de2 [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>
29#include "drmP.h"
30#include "radeon_drm.h"
31#include "radeon_reg.h"
32#include "radeon.h"
33#include "atom.h"
34
35int radeon_debugfs_ib_init(struct radeon_device *rdev);
36
37/*
38 * IB.
39 */
40int radeon_ib_get(struct radeon_device *rdev, struct radeon_ib **ib)
41{
42 struct radeon_fence *fence;
43 struct radeon_ib *nib;
Jerome Glisse91cb91b2010-02-15 21:36:13 +010044 int r = 0, i, c;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020045
46 *ib = NULL;
47 r = radeon_fence_create(rdev, &fence);
48 if (r) {
Jerome Glisse91cb91b2010-02-15 21:36:13 +010049 dev_err(rdev->dev, "failed to create fence for new IB\n");
Jerome Glisse771fe6b2009-06-05 14:42:42 +020050 return r;
51 }
52 mutex_lock(&rdev->ib_pool.mutex);
Jerome Glisse91cb91b2010-02-15 21:36:13 +010053 for (i = rdev->ib_pool.head_id, c = 0, nib = NULL; c < RADEON_IB_POOL_SIZE; c++, i++) {
54 i &= (RADEON_IB_POOL_SIZE - 1);
55 if (rdev->ib_pool.ibs[i].free) {
56 nib = &rdev->ib_pool.ibs[i];
57 break;
58 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +020059 }
Jerome Glisse91cb91b2010-02-15 21:36:13 +010060 if (nib == NULL) {
61 /* This should never happen, it means we allocated all
62 * IB and haven't scheduled one yet, return EBUSY to
63 * userspace hoping that on ioctl recall we get better
64 * luck
65 */
66 dev_err(rdev->dev, "no free indirect buffer !\n");
Dave Airlieecb114a2009-09-15 11:12:56 +100067 mutex_unlock(&rdev->ib_pool.mutex);
Jerome Glisse91cb91b2010-02-15 21:36:13 +010068 radeon_fence_unref(&fence);
69 return -EBUSY;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020070 }
Jerome Glisse91cb91b2010-02-15 21:36:13 +010071 rdev->ib_pool.head_id = (nib->idx + 1) & (RADEON_IB_POOL_SIZE - 1);
72 nib->free = false;
73 if (nib->fence) {
Dave Airlieecb114a2009-09-15 11:12:56 +100074 mutex_unlock(&rdev->ib_pool.mutex);
Jerome Glisse91cb91b2010-02-15 21:36:13 +010075 r = radeon_fence_wait(nib->fence, false);
76 if (r) {
77 dev_err(rdev->dev, "error waiting fence of IB(%u:0x%016lX:%u)\n",
78 nib->idx, (unsigned long)nib->gpu_addr, nib->length_dw);
79 mutex_lock(&rdev->ib_pool.mutex);
80 nib->free = true;
81 mutex_unlock(&rdev->ib_pool.mutex);
82 radeon_fence_unref(&fence);
83 return r;
84 }
85 mutex_lock(&rdev->ib_pool.mutex);
Jerome Glisse771fe6b2009-06-05 14:42:42 +020086 }
87 radeon_fence_unref(&nib->fence);
Jerome Glisse91cb91b2010-02-15 21:36:13 +010088 nib->fence = fence;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020089 nib->length_dw = 0;
Dave Airlieecb114a2009-09-15 11:12:56 +100090 mutex_unlock(&rdev->ib_pool.mutex);
Jerome Glisse771fe6b2009-06-05 14:42:42 +020091 *ib = nib;
Jerome Glisse91cb91b2010-02-15 21:36:13 +010092 return 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020093}
94
95void radeon_ib_free(struct radeon_device *rdev, struct radeon_ib **ib)
96{
97 struct radeon_ib *tmp = *ib;
98
99 *ib = NULL;
100 if (tmp == NULL) {
101 return;
102 }
103 mutex_lock(&rdev->ib_pool.mutex);
Jerome Glisse91cb91b2010-02-15 21:36:13 +0100104 tmp->free = true;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200105 mutex_unlock(&rdev->ib_pool.mutex);
106}
107
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200108int radeon_ib_schedule(struct radeon_device *rdev, struct radeon_ib *ib)
109{
110 int r = 0;
111
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200112 if (!ib->length_dw || !rdev->cp.ready) {
113 /* TODO: Nothings in the ib we should report. */
Jerome Glisse91cb91b2010-02-15 21:36:13 +0100114 DRM_ERROR("radeon: couldn't schedule IB(%u).\n", ib->idx);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200115 return -EINVAL;
116 }
Dave Airlieecb114a2009-09-15 11:12:56 +1000117
Dave Airlie6cdf6582009-06-29 18:29:13 +1000118 /* 64 dwords should be enough for fence too */
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200119 r = radeon_ring_lock(rdev, 64);
120 if (r) {
121 DRM_ERROR("radeon: scheduling IB failled (%d).\n", r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200122 return r;
123 }
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000124 radeon_ring_ib_execute(rdev, ib);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200125 radeon_fence_emit(rdev, ib->fence);
Dave Airlieecb114a2009-09-15 11:12:56 +1000126 mutex_lock(&rdev->ib_pool.mutex);
Jerome Glisse91cb91b2010-02-15 21:36:13 +0100127 /* once scheduled IB is considered free and protected by the fence */
128 ib->free = true;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200129 mutex_unlock(&rdev->ib_pool.mutex);
Dave Airlieecb114a2009-09-15 11:12:56 +1000130 radeon_ring_unlock_commit(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200131 return 0;
132}
133
134int radeon_ib_pool_init(struct radeon_device *rdev)
135{
136 void *ptr;
137 uint64_t gpu_addr;
138 int i;
139 int r = 0;
140
Jerome Glisse9f022dd2009-09-11 15:35:22 +0200141 if (rdev->ib_pool.robj)
142 return 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200143 /* Allocate 1M object buffer */
Jerome Glisse4c788672009-11-20 14:29:23 +0100144 r = radeon_bo_create(rdev, NULL, RADEON_IB_POOL_SIZE*64*1024,
145 true, RADEON_GEM_DOMAIN_GTT,
146 &rdev->ib_pool.robj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200147 if (r) {
148 DRM_ERROR("radeon: failed to ib pool (%d).\n", r);
149 return r;
150 }
Jerome Glisse4c788672009-11-20 14:29:23 +0100151 r = radeon_bo_reserve(rdev->ib_pool.robj, false);
152 if (unlikely(r != 0))
153 return r;
154 r = radeon_bo_pin(rdev->ib_pool.robj, RADEON_GEM_DOMAIN_GTT, &gpu_addr);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200155 if (r) {
Jerome Glisse4c788672009-11-20 14:29:23 +0100156 radeon_bo_unreserve(rdev->ib_pool.robj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200157 DRM_ERROR("radeon: failed to pin ib pool (%d).\n", r);
158 return r;
159 }
Jerome Glisse4c788672009-11-20 14:29:23 +0100160 r = radeon_bo_kmap(rdev->ib_pool.robj, &ptr);
161 radeon_bo_unreserve(rdev->ib_pool.robj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200162 if (r) {
163 DRM_ERROR("radeon: failed to map ib poll (%d).\n", r);
164 return r;
165 }
166 for (i = 0; i < RADEON_IB_POOL_SIZE; i++) {
167 unsigned offset;
168
169 offset = i * 64 * 1024;
170 rdev->ib_pool.ibs[i].gpu_addr = gpu_addr + offset;
171 rdev->ib_pool.ibs[i].ptr = ptr + offset;
172 rdev->ib_pool.ibs[i].idx = i;
173 rdev->ib_pool.ibs[i].length_dw = 0;
Jerome Glisse91cb91b2010-02-15 21:36:13 +0100174 rdev->ib_pool.ibs[i].free = true;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200175 }
Jerome Glisse91cb91b2010-02-15 21:36:13 +0100176 rdev->ib_pool.head_id = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200177 rdev->ib_pool.ready = true;
178 DRM_INFO("radeon: ib pool ready.\n");
179 if (radeon_debugfs_ib_init(rdev)) {
180 DRM_ERROR("Failed to register debugfs file for IB !\n");
181 }
182 return r;
183}
184
185void radeon_ib_pool_fini(struct radeon_device *rdev)
186{
Jerome Glisse4c788672009-11-20 14:29:23 +0100187 int r;
188
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200189 if (!rdev->ib_pool.ready) {
190 return;
191 }
192 mutex_lock(&rdev->ib_pool.mutex);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200193 if (rdev->ib_pool.robj) {
Jerome Glisse4c788672009-11-20 14:29:23 +0100194 r = radeon_bo_reserve(rdev->ib_pool.robj, false);
195 if (likely(r == 0)) {
196 radeon_bo_kunmap(rdev->ib_pool.robj);
197 radeon_bo_unpin(rdev->ib_pool.robj);
198 radeon_bo_unreserve(rdev->ib_pool.robj);
199 }
200 radeon_bo_unref(&rdev->ib_pool.robj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200201 rdev->ib_pool.robj = NULL;
202 }
203 mutex_unlock(&rdev->ib_pool.mutex);
204}
205
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200206
207/*
208 * Ring.
209 */
210void radeon_ring_free_size(struct radeon_device *rdev)
211{
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000212 if (rdev->family >= CHIP_R600)
213 rdev->cp.rptr = RREG32(R600_CP_RB_RPTR);
214 else
215 rdev->cp.rptr = RREG32(RADEON_CP_RB_RPTR);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200216 /* This works because ring_size is a power of 2 */
217 rdev->cp.ring_free_dw = (rdev->cp.rptr + (rdev->cp.ring_size / 4));
218 rdev->cp.ring_free_dw -= rdev->cp.wptr;
219 rdev->cp.ring_free_dw &= rdev->cp.ptr_mask;
220 if (!rdev->cp.ring_free_dw) {
221 rdev->cp.ring_free_dw = rdev->cp.ring_size / 4;
222 }
223}
224
225int radeon_ring_lock(struct radeon_device *rdev, unsigned ndw)
226{
227 int r;
228
229 /* Align requested size with padding so unlock_commit can
230 * pad safely */
231 ndw = (ndw + rdev->cp.align_mask) & ~rdev->cp.align_mask;
232 mutex_lock(&rdev->cp.mutex);
233 while (ndw > (rdev->cp.ring_free_dw - 1)) {
234 radeon_ring_free_size(rdev);
235 if (ndw < rdev->cp.ring_free_dw) {
236 break;
237 }
238 r = radeon_fence_wait_next(rdev);
239 if (r) {
240 mutex_unlock(&rdev->cp.mutex);
241 return r;
242 }
243 }
244 rdev->cp.count_dw = ndw;
245 rdev->cp.wptr_old = rdev->cp.wptr;
246 return 0;
247}
248
249void radeon_ring_unlock_commit(struct radeon_device *rdev)
250{
251 unsigned count_dw_pad;
252 unsigned i;
253
254 /* We pad to match fetch size */
255 count_dw_pad = (rdev->cp.align_mask + 1) -
256 (rdev->cp.wptr & rdev->cp.align_mask);
257 for (i = 0; i < count_dw_pad; i++) {
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000258 radeon_ring_write(rdev, 2 << 30);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200259 }
260 DRM_MEMORYBARRIER();
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000261 radeon_cp_commit(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200262 mutex_unlock(&rdev->cp.mutex);
263}
264
265void radeon_ring_unlock_undo(struct radeon_device *rdev)
266{
267 rdev->cp.wptr = rdev->cp.wptr_old;
268 mutex_unlock(&rdev->cp.mutex);
269}
270
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200271int radeon_ring_init(struct radeon_device *rdev, unsigned ring_size)
272{
273 int r;
274
275 rdev->cp.ring_size = ring_size;
276 /* Allocate ring buffer */
277 if (rdev->cp.ring_obj == NULL) {
Jerome Glisse4c788672009-11-20 14:29:23 +0100278 r = radeon_bo_create(rdev, NULL, rdev->cp.ring_size, true,
279 RADEON_GEM_DOMAIN_GTT,
280 &rdev->cp.ring_obj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200281 if (r) {
Jerome Glisse4c788672009-11-20 14:29:23 +0100282 dev_err(rdev->dev, "(%d) ring create failed\n", r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200283 return r;
284 }
Jerome Glisse4c788672009-11-20 14:29:23 +0100285 r = radeon_bo_reserve(rdev->cp.ring_obj, false);
286 if (unlikely(r != 0))
287 return r;
288 r = radeon_bo_pin(rdev->cp.ring_obj, RADEON_GEM_DOMAIN_GTT,
289 &rdev->cp.gpu_addr);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200290 if (r) {
Jerome Glisse4c788672009-11-20 14:29:23 +0100291 radeon_bo_unreserve(rdev->cp.ring_obj);
292 dev_err(rdev->dev, "(%d) ring pin failed\n", r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200293 return r;
294 }
Jerome Glisse4c788672009-11-20 14:29:23 +0100295 r = radeon_bo_kmap(rdev->cp.ring_obj,
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200296 (void **)&rdev->cp.ring);
Jerome Glisse4c788672009-11-20 14:29:23 +0100297 radeon_bo_unreserve(rdev->cp.ring_obj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200298 if (r) {
Jerome Glisse4c788672009-11-20 14:29:23 +0100299 dev_err(rdev->dev, "(%d) ring map failed\n", r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200300 return r;
301 }
302 }
303 rdev->cp.ptr_mask = (rdev->cp.ring_size / 4) - 1;
304 rdev->cp.ring_free_dw = rdev->cp.ring_size / 4;
305 return 0;
306}
307
308void radeon_ring_fini(struct radeon_device *rdev)
309{
Jerome Glisse4c788672009-11-20 14:29:23 +0100310 int r;
311
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200312 mutex_lock(&rdev->cp.mutex);
313 if (rdev->cp.ring_obj) {
Jerome Glisse4c788672009-11-20 14:29:23 +0100314 r = radeon_bo_reserve(rdev->cp.ring_obj, false);
315 if (likely(r == 0)) {
316 radeon_bo_kunmap(rdev->cp.ring_obj);
317 radeon_bo_unpin(rdev->cp.ring_obj);
318 radeon_bo_unreserve(rdev->cp.ring_obj);
319 }
320 radeon_bo_unref(&rdev->cp.ring_obj);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200321 rdev->cp.ring = NULL;
322 rdev->cp.ring_obj = NULL;
323 }
324 mutex_unlock(&rdev->cp.mutex);
325}
326
327
328/*
329 * Debugfs info
330 */
331#if defined(CONFIG_DEBUG_FS)
332static int radeon_debugfs_ib_info(struct seq_file *m, void *data)
333{
334 struct drm_info_node *node = (struct drm_info_node *) m->private;
335 struct radeon_ib *ib = node->info_ent->data;
336 unsigned i;
337
338 if (ib == NULL) {
339 return 0;
340 }
Jerome Glisse91cb91b2010-02-15 21:36:13 +0100341 seq_printf(m, "IB %04u\n", ib->idx);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200342 seq_printf(m, "IB fence %p\n", ib->fence);
343 seq_printf(m, "IB size %05u dwords\n", ib->length_dw);
344 for (i = 0; i < ib->length_dw; i++) {
345 seq_printf(m, "[%05u]=0x%08X\n", i, ib->ptr[i]);
346 }
347 return 0;
348}
349
350static struct drm_info_list radeon_debugfs_ib_list[RADEON_IB_POOL_SIZE];
351static char radeon_debugfs_ib_names[RADEON_IB_POOL_SIZE][32];
352#endif
353
354int radeon_debugfs_ib_init(struct radeon_device *rdev)
355{
356#if defined(CONFIG_DEBUG_FS)
357 unsigned i;
358
359 for (i = 0; i < RADEON_IB_POOL_SIZE; i++) {
360 sprintf(radeon_debugfs_ib_names[i], "radeon_ib_%04u", i);
361 radeon_debugfs_ib_list[i].name = radeon_debugfs_ib_names[i];
362 radeon_debugfs_ib_list[i].show = &radeon_debugfs_ib_info;
363 radeon_debugfs_ib_list[i].driver_features = 0;
364 radeon_debugfs_ib_list[i].data = &rdev->ib_pool.ibs[i];
365 }
366 return radeon_debugfs_add_files(rdev, radeon_debugfs_ib_list,
367 RADEON_IB_POOL_SIZE);
368#else
369 return 0;
370#endif
371}