1 /**************************************************************************
3 Copyright (C) 2004 Nicolai Haehnle.
4 Copyright (C) The Weather Channel, Inc. 2002. All Rights Reserved.
6 The Weather Channel (TM) funded Tungsten Graphics to develop the
7 initial release of the Radeon 8500 driver under the XFree86 license.
8 This notice must be preserved.
12 Permission is hereby granted, free of charge, to any person obtaining a
13 copy of this software and associated documentation files (the "Software"),
14 to deal in the Software without restriction, including without limitation
15 on the rights to use, copy, modify, merge, publish, distribute, sub
16 license, and/or sell copies of the Software, and to permit persons to whom
17 the Software is furnished to do so, subject to the following conditions:
19 The above copyright notice and this permission notice (including the next
20 paragraph) shall be included in all copies or substantial portions of the
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
26 ATI, VA LINUX SYSTEMS AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
27 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
28 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
29 USE OR OTHER DEALINGS IN THE SOFTWARE.
31 **************************************************************************/
33 #include "radeon_common.h"
34 #include "main/simple_list.h"
36 #if defined(USE_X86_ASM)
37 #define COPY_DWORDS( dst, src, nr ) \
40 __asm__ __volatile__( "rep ; movsl" \
41 : "=%c" (__tmp), "=D" (dst), "=S" (__tmp) \
47 #define COPY_DWORDS( dst, src, nr ) \
50 for ( j = 0 ; j < nr ; j++ ) \
51 dst[j] = ((int *)src)[j]; \
56 void radeonEmitVec4(uint32_t *out
, const GLvoid
* data
, int stride
, int count
)
60 if (RADEON_DEBUG
& DEBUG_VERTS
)
61 fprintf(stderr
, "%s count %d stride %d out %p data %p\n",
62 __FUNCTION__
, count
, stride
, (void *)out
, (void *)data
);
65 COPY_DWORDS(out
, data
, count
);
67 for (i
= 0; i
< count
; i
++) {
68 out
[0] = *(int *)data
;
74 void radeonEmitVec8(uint32_t *out
, const GLvoid
* data
, int stride
, int count
)
78 if (RADEON_DEBUG
& DEBUG_VERTS
)
79 fprintf(stderr
, "%s count %d stride %d out %p data %p\n",
80 __FUNCTION__
, count
, stride
, (void *)out
, (void *)data
);
83 COPY_DWORDS(out
, data
, count
* 2);
85 for (i
= 0; i
< count
; i
++) {
86 out
[0] = *(int *)data
;
87 out
[1] = *(int *)(data
+ 4);
93 void radeonEmitVec12(uint32_t *out
, const GLvoid
* data
, int stride
, int count
)
97 if (RADEON_DEBUG
& DEBUG_VERTS
)
98 fprintf(stderr
, "%s count %d stride %d out %p data %p\n",
99 __FUNCTION__
, count
, stride
, (void *)out
, (void *)data
);
102 COPY_DWORDS(out
, data
, count
* 3);
105 for (i
= 0; i
< count
; i
++) {
106 out
[0] = *(int *)data
;
107 out
[1] = *(int *)(data
+ 4);
108 out
[2] = *(int *)(data
+ 8);
114 void radeonEmitVec16(uint32_t *out
, const GLvoid
* data
, int stride
, int count
)
118 if (RADEON_DEBUG
& DEBUG_VERTS
)
119 fprintf(stderr
, "%s count %d stride %d out %p data %p\n",
120 __FUNCTION__
, count
, stride
, (void *)out
, (void *)data
);
123 COPY_DWORDS(out
, data
, count
* 4);
125 for (i
= 0; i
< count
; i
++) {
126 out
[0] = *(int *)data
;
127 out
[1] = *(int *)(data
+ 4);
128 out
[2] = *(int *)(data
+ 8);
129 out
[3] = *(int *)(data
+ 12);
135 void rcommon_emit_vector(GLcontext
* ctx
, struct radeon_aos
*aos
,
136 const GLvoid
* data
, int size
, int stride
, int count
)
138 radeonContextPtr rmesa
= RADEON_CONTEXT(ctx
);
142 radeonAllocDmaRegion(rmesa
, &aos
->bo
, &aos
->offset
, size
* 4, 32);
146 radeonAllocDmaRegion(rmesa
, &aos
->bo
, &aos
->offset
, size
* count
* 4, 32);
150 aos
->components
= size
;
153 out
= (uint32_t*)((char*)aos
->bo
->ptr
+ aos
->offset
);
155 case 1: radeonEmitVec4(out
, data
, stride
, count
); break;
156 case 2: radeonEmitVec8(out
, data
, stride
, count
); break;
157 case 3: radeonEmitVec12(out
, data
, stride
, count
); break;
158 case 4: radeonEmitVec16(out
, data
, stride
, count
); break;
165 void radeon_init_dma(radeonContextPtr rmesa
)
167 make_empty_list(&rmesa
->dma
.free
);
168 make_empty_list(&rmesa
->dma
.wait
);
169 make_empty_list(&rmesa
->dma
.reserved
);
172 void radeonRefillCurrentDmaRegion(radeonContextPtr rmesa
, int size
)
174 size
= MAX2(size
, MAX_DMA_BUF_SZ
);
176 if (RADEON_DEBUG
& (DEBUG_IOCTL
| DEBUG_DMA
))
177 fprintf(stderr
, "%s\n", __FUNCTION__
);
179 if (rmesa
->dma
.flush
) {
180 rmesa
->dma
.flush(rmesa
->glCtx
);
183 if (is_empty_list(&rmesa
->dma
.free
)) {
184 struct radeon_dma_bo
*dma_bo
= CALLOC(sizeof(struct radeon_dma_bo
));
188 dma_bo
->bo
= radeon_bo_open(rmesa
->radeonScreen
->bom
,
189 0, size
, 4, RADEON_GEM_DOMAIN_GTT
,
193 rcommonFlushCmdBuf(rmesa
, __FUNCTION__
);
196 insert_at_head(&rmesa
->dma
.reserved
, dma_bo
);
198 struct radeon_dma_bo
*dma_bo
= last_elem(&rmesa
->dma
.free
);
199 assert(dma_bo
->bo
->cref
== 1);
200 remove_from_list(dma_bo
);
201 insert_at_head(&rmesa
->dma
.reserved
, dma_bo
);
204 rmesa
->dma
.current_used
= 0;
205 rmesa
->dma
.current_vertexptr
= 0;
207 if (radeon_cs_space_check_with_bo(rmesa
->cmdbuf
.cs
,
208 first_elem(&rmesa
->dma
.reserved
)->bo
,
209 RADEON_GEM_DOMAIN_GTT
, 0))
210 fprintf(stderr
,"failure to revalidate BOs - badness\n");
212 if (is_empty_list(&rmesa
->dma
.reserved
)) {
213 /* Cmd buff have been flushed in radeon_revalidate_bos */
217 radeon_bo_map(first_elem(&rmesa
->dma
.reserved
)->bo
, 1);
220 /* Allocates a region from rmesa->dma.current. If there isn't enough
221 * space in current, grab a new buffer (and discard what was left of current)
223 void radeonAllocDmaRegion(radeonContextPtr rmesa
,
224 struct radeon_bo
**pbo
, int *poffset
,
225 int bytes
, int alignment
)
227 if (RADEON_DEBUG
& DEBUG_IOCTL
)
228 fprintf(stderr
, "%s %d\n", __FUNCTION__
, bytes
);
230 if (rmesa
->dma
.flush
)
231 rmesa
->dma
.flush(rmesa
->glCtx
);
233 assert(rmesa
->dma
.current_used
== rmesa
->dma
.current_vertexptr
);
236 rmesa
->dma
.current_used
= (rmesa
->dma
.current_used
+ alignment
) & ~alignment
;
238 if (is_empty_list(&rmesa
->dma
.reserved
)
239 || rmesa
->dma
.current_used
+ bytes
> first_elem(&rmesa
->dma
.reserved
)->bo
->size
)
240 radeonRefillCurrentDmaRegion(rmesa
, (bytes
+ 15) & ~15);
242 *poffset
= rmesa
->dma
.current_used
;
243 *pbo
= first_elem(&rmesa
->dma
.reserved
)->bo
;
246 /* Always align to at least 16 bytes */
247 rmesa
->dma
.current_used
= (rmesa
->dma
.current_used
+ bytes
+ 15) & ~15;
248 rmesa
->dma
.current_vertexptr
= rmesa
->dma
.current_used
;
250 assert(rmesa
->dma
.current_used
<= first_elem(&rmesa
->dma
.reserved
)->bo
->size
);
253 void radeonFreeDmaRegions(radeonContextPtr rmesa
)
255 struct radeon_dma_bo
*dma_bo
;
256 struct radeon_dma_bo
*temp
;
257 if (RADEON_DEBUG
& DEBUG_IOCTL
)
258 fprintf(stderr
, "%s\n", __FUNCTION__
);
260 foreach_s(dma_bo
, temp
, &rmesa
->dma
.free
) {
261 remove_from_list(dma_bo
);
262 radeon_bo_unref(dma_bo
->bo
);
266 foreach_s(dma_bo
, temp
, &rmesa
->dma
.reserved
) {
267 remove_from_list(dma_bo
);
268 radeon_bo_unmap(dma_bo
->bo
);
269 radeon_bo_unref(dma_bo
->bo
);
274 void radeonReleaseDmaRegions(radeonContextPtr rmesa
)
276 struct radeon_dma_bo
*dma_bo
;
277 struct radeon_dma_bo
*temp
;
278 const int expire_at
= ++rmesa
->dma
.free
.expire_counter
+ DMA_BO_FREE_TIME
;
279 const int time
= rmesa
->dma
.free
.expire_counter
;
280 if (RADEON_DEBUG
& DEBUG_IOCTL
)
281 fprintf(stderr
, "%s\n", __FUNCTION__
);
283 /* move waiting bos to free list.
284 wait list provides gpu time to handle data before reuse */
285 foreach_s(dma_bo
, temp
, &rmesa
->dma
.wait
) {
286 if (dma_bo
->expire_counter
== time
) {
287 WARN_ONCE("Leaking dma buffer object!\n");
288 radeon_bo_unref(dma_bo
->bo
);
289 remove_from_list(dma_bo
);
293 if (dma_bo
->bo
->cref
> 1)
295 remove_from_list(dma_bo
);
296 dma_bo
->expire_counter
= expire_at
;
297 insert_at_tail(&rmesa
->dma
.free
, dma_bo
);
300 /* unmap the last dma region */
301 if (!is_empty_list(&rmesa
->dma
.reserved
))
302 radeon_bo_unmap(first_elem(&rmesa
->dma
.reserved
)->bo
);
303 /* move reserved to wait list */
304 foreach_s(dma_bo
, temp
, &rmesa
->dma
.reserved
) {
305 remove_from_list(dma_bo
);
306 dma_bo
->expire_counter
= expire_at
;
307 insert_at_tail(&rmesa
->dma
.wait
, dma_bo
);
310 /* free bos that have been unused for some time */
311 foreach_s(dma_bo
, temp
, &rmesa
->dma
.free
) {
312 if (dma_bo
->expire_counter
!= time
)
314 remove_from_list(dma_bo
);
315 radeon_bo_unref(dma_bo
->bo
);
322 /* Flush vertices in the current dma region.
324 void rcommon_flush_last_swtcl_prim( GLcontext
*ctx
)
326 radeonContextPtr rmesa
= RADEON_CONTEXT(ctx
);
327 struct radeon_dma
*dma
= &rmesa
->dma
;
330 if (RADEON_DEBUG
& DEBUG_IOCTL
)
331 fprintf(stderr
, "%s\n", __FUNCTION__
);
334 if (!is_empty_list(&dma
->reserved
)) {
335 GLuint current_offset
= dma
->current_used
;
337 assert (dma
->current_used
+
338 rmesa
->swtcl
.numverts
* rmesa
->swtcl
.vertex_size
* 4 ==
339 dma
->current_vertexptr
);
341 if (dma
->current_used
!= dma
->current_vertexptr
) {
342 dma
->current_used
= dma
->current_vertexptr
;
344 rmesa
->vtbl
.swtcl_flush(ctx
, current_offset
);
346 rmesa
->swtcl
.numverts
= 0;
349 /* Alloc space in the current dma region.
352 rcommonAllocDmaLowVerts( radeonContextPtr rmesa
, int nverts
, int vsize
)
354 GLuint bytes
= vsize
* nverts
;
357 if (RADEON_DEBUG
& DEBUG_IOCTL
)
358 fprintf(stderr
, "%s\n", __FUNCTION__
);
359 if (is_empty_list(&rmesa
->dma
.reserved
)
360 || rmesa
->dma
.current_vertexptr
+ bytes
> first_elem(&rmesa
->dma
.reserved
)->bo
->size
) {
361 radeonRefillCurrentDmaRegion(rmesa
, bytes
);
364 if (!rmesa
->dma
.flush
) {
365 /* make sure we have enough space to use this in cmdbuf */
366 rcommonEnsureCmdBufSpace(rmesa
,
367 rmesa
->hw
.max_state_size
+ (20*sizeof(int)),
369 /* if cmdbuf flushed DMA restart */
370 if (is_empty_list(&rmesa
->dma
.reserved
))
372 rmesa
->glCtx
->Driver
.NeedFlush
|= FLUSH_STORED_VERTICES
;
373 rmesa
->dma
.flush
= rcommon_flush_last_swtcl_prim
;
376 ASSERT( vsize
== rmesa
->swtcl
.vertex_size
* 4 );
377 ASSERT( rmesa
->dma
.flush
== rcommon_flush_last_swtcl_prim
);
378 ASSERT( rmesa
->dma
.current_used
+
379 rmesa
->swtcl
.numverts
* rmesa
->swtcl
.vertex_size
* 4 ==
380 rmesa
->dma
.current_vertexptr
);
382 head
= (first_elem(&rmesa
->dma
.reserved
)->bo
->ptr
+ rmesa
->dma
.current_vertexptr
);
383 rmesa
->dma
.current_vertexptr
+= bytes
;
384 rmesa
->swtcl
.numverts
+= nverts
;
388 void radeonReleaseArrays( GLcontext
*ctx
, GLuint newinputs
)
390 radeonContextPtr radeon
= RADEON_CONTEXT( ctx
);
392 if (RADEON_DEBUG
& DEBUG_IOCTL
)
393 fprintf(stderr
, "%s\n", __FUNCTION__
);
395 if (radeon
->dma
.flush
) {
396 radeon
->dma
.flush(radeon
->glCtx
);
398 for (i
= 0; i
< radeon
->tcl
.aos_count
; i
++) {
399 if (radeon
->tcl
.aos
[i
].bo
) {
400 radeon_bo_unref(radeon
->tcl
.aos
[i
].bo
);
401 radeon
->tcl
.aos
[i
].bo
= NULL
;