drm/radeon: prevent crash in the ring space allocation
[linux/fpc-iii.git] / drivers / gpu / drm / radeon / radeon_ring.c
blob65be5e8aaee2d23622bed7c6c0edec6ececd6da2
1 /*
2 * Copyright 2008 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 * Copyright 2009 Jerome Glisse.
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 * Authors: Dave Airlie
25 * Alex Deucher
26 * Jerome Glisse
28 #include <linux/seq_file.h>
29 #include <linux/slab.h>
30 #include "drmP.h"
31 #include "radeon_drm.h"
32 #include "radeon_reg.h"
33 #include "radeon.h"
34 #include "atom.h"
36 int radeon_debugfs_ib_init(struct radeon_device *rdev);
38 u32 radeon_get_ib_value(struct radeon_cs_parser *p, int idx)
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;
45 pg_idx = (idx * 4) / PAGE_SIZE;
46 pg_offset = (idx * 4) % PAGE_SIZE;
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];
53 new_page = radeon_cs_update_pages(p, pg_idx);
54 if (new_page < 0) {
55 p->parser_error = new_page;
56 return 0;
59 idx_value = ibc->kpage[new_page][pg_offset/4];
60 return idx_value;
63 void radeon_ring_write(struct radeon_device *rdev, uint32_t v)
65 #if DRM_DEBUG_CODE
66 if (rdev->cp.count_dw <= 0) {
67 DRM_ERROR("radeon: writting more dword to ring than expected !\n");
69 #endif
70 rdev->cp.ring[rdev->cp.wptr++] = v;
71 rdev->cp.wptr &= rdev->cp.ptr_mask;
72 rdev->cp.count_dw--;
73 rdev->cp.ring_free_dw--;
76 void radeon_ib_bogus_cleanup(struct radeon_device *rdev)
78 struct radeon_ib *ib, *n;
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);
87 void radeon_ib_bogus_add(struct radeon_device *rdev, struct radeon_ib *ib)
89 struct radeon_ib *bib;
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;
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);
107 * IB.
109 int radeon_ib_get(struct radeon_device *rdev, struct radeon_ib **ib)
111 struct radeon_fence *fence;
112 struct radeon_ib *nib;
113 int r = 0, i, c;
115 *ib = NULL;
116 r = radeon_fence_create(rdev, &fence);
117 if (r) {
118 dev_err(rdev->dev, "failed to create fence for new IB\n");
119 return r;
121 mutex_lock(&rdev->ib_pool.mutex);
122 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;
129 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
135 dev_err(rdev->dev, "no free indirect buffer !\n");
136 mutex_unlock(&rdev->ib_pool.mutex);
137 radeon_fence_unref(&fence);
138 return -EBUSY;
140 rdev->ib_pool.head_id = (nib->idx + 1) & (RADEON_IB_POOL_SIZE - 1);
141 nib->free = false;
142 if (nib->fence) {
143 mutex_unlock(&rdev->ib_pool.mutex);
144 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;
154 mutex_lock(&rdev->ib_pool.mutex);
156 radeon_fence_unref(&nib->fence);
157 nib->fence = fence;
158 nib->length_dw = 0;
159 mutex_unlock(&rdev->ib_pool.mutex);
160 *ib = nib;
161 return 0;
164 void radeon_ib_free(struct radeon_device *rdev, struct radeon_ib **ib)
166 struct radeon_ib *tmp = *ib;
168 *ib = NULL;
169 if (tmp == NULL) {
170 return;
172 if (!tmp->fence->emited)
173 radeon_fence_unref(&tmp->fence);
174 mutex_lock(&rdev->ib_pool.mutex);
175 tmp->free = true;
176 mutex_unlock(&rdev->ib_pool.mutex);
179 int radeon_ib_schedule(struct radeon_device *rdev, struct radeon_ib *ib)
181 int r = 0;
183 if (!ib->length_dw || !rdev->cp.ready) {
184 /* TODO: Nothings in the ib we should report. */
185 DRM_ERROR("radeon: couldn't schedule IB(%u).\n", ib->idx);
186 return -EINVAL;
189 /* 64 dwords should be enough for fence too */
190 r = radeon_ring_lock(rdev, 64);
191 if (r) {
192 DRM_ERROR("radeon: scheduling IB failed (%d).\n", r);
193 return r;
195 radeon_ring_ib_execute(rdev, ib);
196 radeon_fence_emit(rdev, ib->fence);
197 mutex_lock(&rdev->ib_pool.mutex);
198 /* once scheduled IB is considered free and protected by the fence */
199 ib->free = true;
200 mutex_unlock(&rdev->ib_pool.mutex);
201 radeon_ring_unlock_commit(rdev);
202 return 0;
205 int radeon_ib_pool_init(struct radeon_device *rdev)
207 void *ptr;
208 uint64_t gpu_addr;
209 int i;
210 int r = 0;
212 if (rdev->ib_pool.robj)
213 return 0;
214 INIT_LIST_HEAD(&rdev->ib_pool.bogus_ib);
215 /* Allocate 1M object buffer */
216 r = radeon_bo_create(rdev, RADEON_IB_POOL_SIZE*64*1024,
217 PAGE_SIZE, true, RADEON_GEM_DOMAIN_GTT,
218 &rdev->ib_pool.robj);
219 if (r) {
220 DRM_ERROR("radeon: failed to ib pool (%d).\n", r);
221 return r;
223 r = radeon_bo_reserve(rdev->ib_pool.robj, false);
224 if (unlikely(r != 0))
225 return r;
226 r = radeon_bo_pin(rdev->ib_pool.robj, RADEON_GEM_DOMAIN_GTT, &gpu_addr);
227 if (r) {
228 radeon_bo_unreserve(rdev->ib_pool.robj);
229 DRM_ERROR("radeon: failed to pin ib pool (%d).\n", r);
230 return r;
232 r = radeon_bo_kmap(rdev->ib_pool.robj, &ptr);
233 radeon_bo_unreserve(rdev->ib_pool.robj);
234 if (r) {
235 DRM_ERROR("radeon: failed to map ib pool (%d).\n", r);
236 return r;
238 for (i = 0; i < RADEON_IB_POOL_SIZE; i++) {
239 unsigned offset;
241 offset = i * 64 * 1024;
242 rdev->ib_pool.ibs[i].gpu_addr = gpu_addr + offset;
243 rdev->ib_pool.ibs[i].ptr = ptr + offset;
244 rdev->ib_pool.ibs[i].idx = i;
245 rdev->ib_pool.ibs[i].length_dw = 0;
246 rdev->ib_pool.ibs[i].free = true;
248 rdev->ib_pool.head_id = 0;
249 rdev->ib_pool.ready = true;
250 DRM_INFO("radeon: ib pool ready.\n");
251 if (radeon_debugfs_ib_init(rdev)) {
252 DRM_ERROR("Failed to register debugfs file for IB !\n");
254 return r;
257 void radeon_ib_pool_fini(struct radeon_device *rdev)
259 int r;
260 struct radeon_bo *robj;
262 if (!rdev->ib_pool.ready) {
263 return;
265 mutex_lock(&rdev->ib_pool.mutex);
266 radeon_ib_bogus_cleanup(rdev);
267 robj = rdev->ib_pool.robj;
268 rdev->ib_pool.robj = NULL;
269 mutex_unlock(&rdev->ib_pool.mutex);
271 if (robj) {
272 r = radeon_bo_reserve(robj, false);
273 if (likely(r == 0)) {
274 radeon_bo_kunmap(robj);
275 radeon_bo_unpin(robj);
276 radeon_bo_unreserve(robj);
278 radeon_bo_unref(&robj);
284 * Ring.
286 void radeon_ring_free_size(struct radeon_device *rdev)
288 if (rdev->wb.enabled)
289 rdev->cp.rptr = le32_to_cpu(rdev->wb.wb[RADEON_WB_CP_RPTR_OFFSET/4]);
290 else {
291 if (rdev->family >= CHIP_R600)
292 rdev->cp.rptr = RREG32(R600_CP_RB_RPTR);
293 else
294 rdev->cp.rptr = RREG32(RADEON_CP_RB_RPTR);
296 /* This works because ring_size is a power of 2 */
297 rdev->cp.ring_free_dw = (rdev->cp.rptr + (rdev->cp.ring_size / 4));
298 rdev->cp.ring_free_dw -= rdev->cp.wptr;
299 rdev->cp.ring_free_dw &= rdev->cp.ptr_mask;
300 if (!rdev->cp.ring_free_dw) {
301 rdev->cp.ring_free_dw = rdev->cp.ring_size / 4;
305 int radeon_ring_alloc(struct radeon_device *rdev, unsigned ndw)
307 int r;
309 /* make sure we aren't trying to allocate more space than there is on the ring */
310 if (ndw > (rdev->cp.ring_size / 4))
311 return -ENOMEM;
312 /* Align requested size with padding so unlock_commit can
313 * pad safely */
314 ndw = (ndw + rdev->cp.align_mask) & ~rdev->cp.align_mask;
315 while (ndw > (rdev->cp.ring_free_dw - 1)) {
316 radeon_ring_free_size(rdev);
317 if (ndw < rdev->cp.ring_free_dw) {
318 break;
320 r = radeon_fence_wait_next(rdev);
321 if (r)
322 return r;
324 rdev->cp.count_dw = ndw;
325 rdev->cp.wptr_old = rdev->cp.wptr;
326 return 0;
329 int radeon_ring_lock(struct radeon_device *rdev, unsigned ndw)
331 int r;
333 mutex_lock(&rdev->cp.mutex);
334 r = radeon_ring_alloc(rdev, ndw);
335 if (r) {
336 mutex_unlock(&rdev->cp.mutex);
337 return r;
339 return 0;
342 void radeon_ring_commit(struct radeon_device *rdev)
344 unsigned count_dw_pad;
345 unsigned i;
347 /* We pad to match fetch size */
348 count_dw_pad = (rdev->cp.align_mask + 1) -
349 (rdev->cp.wptr & rdev->cp.align_mask);
350 for (i = 0; i < count_dw_pad; i++) {
351 radeon_ring_write(rdev, 2 << 30);
353 DRM_MEMORYBARRIER();
354 radeon_cp_commit(rdev);
357 void radeon_ring_unlock_commit(struct radeon_device *rdev)
359 radeon_ring_commit(rdev);
360 mutex_unlock(&rdev->cp.mutex);
363 void radeon_ring_unlock_undo(struct radeon_device *rdev)
365 rdev->cp.wptr = rdev->cp.wptr_old;
366 mutex_unlock(&rdev->cp.mutex);
369 int radeon_ring_init(struct radeon_device *rdev, unsigned ring_size)
371 int r;
373 rdev->cp.ring_size = ring_size;
374 /* Allocate ring buffer */
375 if (rdev->cp.ring_obj == NULL) {
376 r = radeon_bo_create(rdev, rdev->cp.ring_size, PAGE_SIZE, true,
377 RADEON_GEM_DOMAIN_GTT,
378 &rdev->cp.ring_obj);
379 if (r) {
380 dev_err(rdev->dev, "(%d) ring create failed\n", r);
381 return r;
383 r = radeon_bo_reserve(rdev->cp.ring_obj, false);
384 if (unlikely(r != 0))
385 return r;
386 r = radeon_bo_pin(rdev->cp.ring_obj, RADEON_GEM_DOMAIN_GTT,
387 &rdev->cp.gpu_addr);
388 if (r) {
389 radeon_bo_unreserve(rdev->cp.ring_obj);
390 dev_err(rdev->dev, "(%d) ring pin failed\n", r);
391 return r;
393 r = radeon_bo_kmap(rdev->cp.ring_obj,
394 (void **)&rdev->cp.ring);
395 radeon_bo_unreserve(rdev->cp.ring_obj);
396 if (r) {
397 dev_err(rdev->dev, "(%d) ring map failed\n", r);
398 return r;
401 rdev->cp.ptr_mask = (rdev->cp.ring_size / 4) - 1;
402 rdev->cp.ring_free_dw = rdev->cp.ring_size / 4;
403 return 0;
406 void radeon_ring_fini(struct radeon_device *rdev)
408 int r;
409 struct radeon_bo *ring_obj;
411 mutex_lock(&rdev->cp.mutex);
412 ring_obj = rdev->cp.ring_obj;
413 rdev->cp.ring = NULL;
414 rdev->cp.ring_obj = NULL;
415 mutex_unlock(&rdev->cp.mutex);
417 if (ring_obj) {
418 r = radeon_bo_reserve(ring_obj, false);
419 if (likely(r == 0)) {
420 radeon_bo_kunmap(ring_obj);
421 radeon_bo_unpin(ring_obj);
422 radeon_bo_unreserve(ring_obj);
424 radeon_bo_unref(&ring_obj);
430 * Debugfs info
432 #if defined(CONFIG_DEBUG_FS)
433 static int radeon_debugfs_ib_info(struct seq_file *m, void *data)
435 struct drm_info_node *node = (struct drm_info_node *) m->private;
436 struct radeon_ib *ib = node->info_ent->data;
437 unsigned i;
439 if (ib == NULL) {
440 return 0;
442 seq_printf(m, "IB %04u\n", ib->idx);
443 seq_printf(m, "IB fence %p\n", ib->fence);
444 seq_printf(m, "IB size %05u dwords\n", ib->length_dw);
445 for (i = 0; i < ib->length_dw; i++) {
446 seq_printf(m, "[%05u]=0x%08X\n", i, ib->ptr[i]);
448 return 0;
451 static int radeon_debugfs_ib_bogus_info(struct seq_file *m, void *data)
453 struct drm_info_node *node = (struct drm_info_node *) m->private;
454 struct radeon_device *rdev = node->info_ent->data;
455 struct radeon_ib *ib;
456 unsigned i;
458 mutex_lock(&rdev->ib_pool.mutex);
459 if (list_empty(&rdev->ib_pool.bogus_ib)) {
460 mutex_unlock(&rdev->ib_pool.mutex);
461 seq_printf(m, "no bogus IB recorded\n");
462 return 0;
464 ib = list_first_entry(&rdev->ib_pool.bogus_ib, struct radeon_ib, list);
465 list_del_init(&ib->list);
466 mutex_unlock(&rdev->ib_pool.mutex);
467 seq_printf(m, "IB size %05u dwords\n", ib->length_dw);
468 for (i = 0; i < ib->length_dw; i++) {
469 seq_printf(m, "[%05u]=0x%08X\n", i, ib->ptr[i]);
471 vfree(ib->ptr);
472 kfree(ib);
473 return 0;
476 static struct drm_info_list radeon_debugfs_ib_list[RADEON_IB_POOL_SIZE];
477 static char radeon_debugfs_ib_names[RADEON_IB_POOL_SIZE][32];
479 static struct drm_info_list radeon_debugfs_ib_bogus_info_list[] = {
480 {"radeon_ib_bogus", radeon_debugfs_ib_bogus_info, 0, NULL},
482 #endif
484 int radeon_debugfs_ib_init(struct radeon_device *rdev)
486 #if defined(CONFIG_DEBUG_FS)
487 unsigned i;
488 int r;
490 radeon_debugfs_ib_bogus_info_list[0].data = rdev;
491 r = radeon_debugfs_add_files(rdev, radeon_debugfs_ib_bogus_info_list, 1);
492 if (r)
493 return r;
494 for (i = 0; i < RADEON_IB_POOL_SIZE; i++) {
495 sprintf(radeon_debugfs_ib_names[i], "radeon_ib_%04u", i);
496 radeon_debugfs_ib_list[i].name = radeon_debugfs_ib_names[i];
497 radeon_debugfs_ib_list[i].show = &radeon_debugfs_ib_info;
498 radeon_debugfs_ib_list[i].driver_features = 0;
499 radeon_debugfs_ib_list[i].data = &rdev->ib_pool.ibs[i];
501 return radeon_debugfs_add_files(rdev, radeon_debugfs_ib_list,
502 RADEON_IB_POOL_SIZE);
503 #else
504 return 0;
505 #endif