2
* Copyright © 2009 Red Hat, Inc.
4
* Permission is hereby granted, free of charge, to any person obtaining a
5
* copy of this software and associated documentation files (the "Software"),
6
* to deal in the Software without restriction, including without limitation
7
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
8
* and/or sell copies of the Software, and to permit persons to whom the
9
* Software is furnished to do so, subject to the following conditions:
11
* The above copyright notice and this permission notice (including the next
12
* paragraph) shall be included in all copies or substantial portions of the
15
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
* Dave Airlie <airlied@redhat.com>
33
#include "radeon_bo.h"
34
#include "radeon_cs.h"
36
#define VBO_SIZE (16*1024)
38
/* KMS vertex buffer support - for R600 only but could be used on previous gpus */
40
static struct radeon_bo *radeon_vbo_get_bo(ScrnInfoPtr pScrn);
42
void radeon_vbo_put(ScrnInfoPtr pScrn, struct radeon_vbo_object *vbo)
46
radeon_bo_unmap(vbo->vb_bo);
47
radeon_bo_unref(vbo->vb_bo);
55
void radeon_vbo_get(ScrnInfoPtr pScrn, struct radeon_vbo_object *vbo)
59
vbo->vb_bo = radeon_vbo_get_bo(pScrn);
61
radeon_bo_ref(vbo->vb_bo);
62
ret = radeon_bo_map(vbo->vb_bo, 1);
64
FatalError("Failed to map vb %d\n", ret);
67
vbo->vb_total = VBO_SIZE;
69
vbo->vb_start_op = vbo->vb_offset;
72
/* these functions could migrate to libdrm and
73
be shared with the radeon 3D driver */
74
static int radeon_bo_is_idle(struct radeon_bo *bo)
77
int ret = radeon_bo_is_busy(bo, &domain);
81
void radeon_vbo_init_lists(ScrnInfoPtr pScrn)
83
RADEONInfoPtr info = RADEONPTR(pScrn);
84
struct radeon_accel_state *accel_state = info->accel_state;
86
accel_state->use_vbos = TRUE;
87
make_empty_list(&accel_state->bo_free);
88
make_empty_list(&accel_state->bo_wait);
89
make_empty_list(&accel_state->bo_reserved);
92
void radeon_vbo_free_lists(ScrnInfoPtr pScrn)
94
RADEONInfoPtr info = RADEONPTR(pScrn);
95
struct radeon_accel_state *accel_state = info->accel_state;
96
struct radeon_dma_bo *dma_bo, *temp;
98
foreach_s(dma_bo, temp, &accel_state->bo_free) {
99
remove_from_list(dma_bo);
100
radeon_bo_unref(dma_bo->bo);
104
foreach_s(dma_bo, temp, &accel_state->bo_wait) {
105
remove_from_list(dma_bo);
106
radeon_bo_unref(dma_bo->bo);
110
foreach_s(dma_bo, temp, &accel_state->bo_reserved) {
111
remove_from_list(dma_bo);
112
radeon_bo_unref(dma_bo->bo);
117
void radeon_vbo_flush_bos(ScrnInfoPtr pScrn)
119
RADEONInfoPtr info = RADEONPTR(pScrn);
120
struct radeon_accel_state *accel_state = info->accel_state;
121
struct radeon_dma_bo *dma_bo, *temp;
122
const int expire_at = ++accel_state->bo_free.expire_counter + DMA_BO_FREE_TIME;
123
const int time = accel_state->bo_free.expire_counter;
125
foreach_s(dma_bo, temp, &accel_state->bo_wait) {
126
if (dma_bo->expire_counter == time) {
127
ErrorF("leaking dma buffer\n");
128
while ((dma_bo->bo = radeon_bo_unref(dma_bo->bo))) {}
129
remove_from_list(dma_bo);
134
if (!radeon_bo_is_idle(dma_bo->bo))
137
if (dma_bo->bo->ptr) {
138
ErrorF("bo with pointer on wait list!\n");
142
remove_from_list(dma_bo);
143
dma_bo->expire_counter = expire_at;
144
insert_at_tail(&accel_state->bo_free, dma_bo);
147
/* move reserved to wait list */
148
foreach_s(dma_bo, temp, &accel_state->bo_reserved) {
149
remove_from_list(dma_bo);
150
dma_bo->expire_counter = expire_at;
151
insert_at_tail(&accel_state->bo_wait, dma_bo);
154
/* free bos that have been unused */
155
foreach_s(dma_bo, temp, &accel_state->bo_free) {
156
if (dma_bo->expire_counter != time)
158
/* always keep one hanging around at end */
159
if (at_end(&accel_state->bo_free, dma_bo)) {
160
dma_bo->expire_counter = time + DMA_BO_FREE_TIME;
164
remove_from_list(dma_bo);
165
radeon_bo_unref(dma_bo->bo);
170
static struct radeon_bo *radeon_vbo_get_bo(ScrnInfoPtr pScrn)
172
RADEONInfoPtr info = RADEONPTR(pScrn);
173
struct radeon_accel_state *accel_state = info->accel_state;
174
struct radeon_dma_bo *dma_bo = NULL;
175
struct radeon_bo *bo;
177
if (is_empty_list(&accel_state->bo_free)) {
178
dma_bo = calloc(1, sizeof(struct radeon_dma_bo));
183
dma_bo->bo = radeon_bo_open(info->bufmgr, 0, VBO_SIZE,
184
0, RADEON_GEM_DOMAIN_GTT, 0);
187
ErrorF("failure to allocate DMA BO\n");
190
insert_at_head(&accel_state->bo_reserved, dma_bo);
192
dma_bo = last_elem(&accel_state->bo_free);
193
remove_from_list(dma_bo);
194
insert_at_head(&accel_state->bo_reserved, dma_bo);
197
if (is_empty_list(&accel_state->bo_reserved))
200
bo = first_elem(&accel_state->bo_reserved)->bo;
202
/* need a space check */
203
if (radeon_cs_space_check_with_bo(info->cs,
205
RADEON_GEM_DOMAIN_GTT, 0))
206
ErrorF("failed to revalidate\n");