2 Copyright (C) The Weather Channel, Inc. 2002. All Rights Reserved.
4 The Weather Channel (TM) funded Tungsten Graphics to develop the
5 initial release of the Radeon 8500 driver under the XFree86 license.
6 This notice must be preserved.
8 Permission is hereby granted, free of charge, to any person obtaining
9 a copy of this software and associated documentation files (the
10 "Software"), to deal in the Software without restriction, including
11 without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense, and/or sell copies of the Software, and to
13 permit persons to whom the Software is furnished to do so, subject to
14 the following conditions:
16 The above copyright notice and this permission notice (including the
17 next paragraph) shall be included in all copies or substantial
18 portions of the Software.
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
24 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 **************************************************************************/
32 * Keith Whitwell <keith@tungstengraphics.com>
38 #include "main/glheader.h"
39 #include "main/imports.h"
40 #include "main/macros.h"
41 #include "main/context.h"
42 #include "swrast/swrast.h"
46 #include "radeon_common.h"
47 #include "radeon_lock.h"
48 #include "r200_context.h"
49 #include "r200_ioctl.h"
50 #include "radeon_reg.h"
54 #define R200_TIMEOUT 512
55 #define R200_IDLE_RETRY 16
57 static void r200KernelClear(GLcontext
*ctx
, GLuint flags
)
59 r200ContextPtr rmesa
= R200_CONTEXT(ctx
);
60 __DRIdrawable
*dPriv
= radeon_get_drawable(&rmesa
->radeon
);
61 GLint cx
, cy
, cw
, ch
, ret
;
64 radeonEmitState(&rmesa
->radeon
);
66 LOCK_HARDWARE( &rmesa
->radeon
);
68 /* Throttle the number of clear ioctls we do.
71 drm_radeon_getparam_t gp
;
75 gp
.param
= RADEON_PARAM_LAST_CLEAR
;
76 gp
.value
= (int *)&clear
;
77 ret
= drmCommandWriteRead( rmesa
->radeon
.dri
.fd
,
78 DRM_RADEON_GETPARAM
, &gp
, sizeof(gp
) );
81 fprintf( stderr
, "%s: drmRadeonGetParam: %d\n", __FUNCTION__
, ret
);
85 /* Clear throttling needs more thought.
87 if ( rmesa
->radeon
.sarea
->last_clear
- clear
<= 25 ) {
91 if (rmesa
->radeon
.do_usleeps
) {
92 UNLOCK_HARDWARE( &rmesa
->radeon
);
94 LOCK_HARDWARE( &rmesa
->radeon
);
98 /* Send current state to the hardware */
99 rcommonFlushCmdBufLocked( &rmesa
->radeon
, __FUNCTION__
);
102 /* compute region after locking: */
103 cx
= ctx
->DrawBuffer
->_Xmin
;
104 cy
= ctx
->DrawBuffer
->_Ymin
;
105 cw
= ctx
->DrawBuffer
->_Xmax
- cx
;
106 ch
= ctx
->DrawBuffer
->_Ymax
- cy
;
108 /* Flip top to bottom */
110 cy
= dPriv
->y
+ dPriv
->h
- cy
- ch
;
111 for ( i
= 0 ; i
< dPriv
->numClipRects
; ) {
112 GLint nr
= MIN2( i
+ RADEON_NR_SAREA_CLIPRECTS
, dPriv
->numClipRects
);
113 drm_clip_rect_t
*box
= dPriv
->pClipRects
;
114 drm_clip_rect_t
*b
= rmesa
->radeon
.sarea
->boxes
;
115 drm_radeon_clear_t clear
;
116 drm_radeon_clear_rect_t depth_boxes
[RADEON_NR_SAREA_CLIPRECTS
];
119 if (cw
!= dPriv
->w
|| ch
!= dPriv
->h
) {
120 /* clear subregion */
121 for ( ; i
< nr
; i
++ ) {
124 GLint w
= box
[i
].x2
- x
;
125 GLint h
= box
[i
].y2
- y
;
127 if ( x
< cx
) w
-= cx
- x
, x
= cx
;
128 if ( y
< cy
) h
-= cy
- y
, y
= cy
;
129 if ( x
+ w
> cx
+ cw
) w
= cx
+ cw
- x
;
130 if ( y
+ h
> cy
+ ch
) h
= cy
+ ch
- y
;
131 if ( w
<= 0 ) continue;
132 if ( h
<= 0 ) continue;
142 /* clear whole window */
143 for ( ; i
< nr
; i
++ ) {
149 rmesa
->radeon
.sarea
->nbox
= n
;
152 clear
.clear_color
= rmesa
->radeon
.state
.color
.clear
;
153 clear
.clear_depth
= rmesa
->radeon
.state
.depth
.clear
; /* needed for hyperz */
154 clear
.color_mask
= rmesa
->hw
.msk
.cmd
[MSK_RB3D_PLANEMASK
];
155 clear
.depth_mask
= rmesa
->radeon
.state
.stencil
.clear
;
156 clear
.depth_boxes
= depth_boxes
;
159 b
= rmesa
->radeon
.sarea
->boxes
;
160 for ( ; n
>= 0 ; n
-- ) {
161 depth_boxes
[n
].f
[CLEAR_X1
] = (float)b
[n
].x1
;
162 depth_boxes
[n
].f
[CLEAR_Y1
] = (float)b
[n
].y1
;
163 depth_boxes
[n
].f
[CLEAR_X2
] = (float)b
[n
].x2
;
164 depth_boxes
[n
].f
[CLEAR_Y2
] = (float)b
[n
].y2
;
165 depth_boxes
[n
].f
[CLEAR_DEPTH
] = ctx
->Depth
.Clear
;
168 ret
= drmCommandWrite( rmesa
->radeon
.dri
.fd
, DRM_RADEON_CLEAR
,
169 &clear
, sizeof(clear
));
173 UNLOCK_HARDWARE( &rmesa
->radeon
);
174 fprintf( stderr
, "DRM_RADEON_CLEAR: return = %d\n", ret
);
178 UNLOCK_HARDWARE( &rmesa
->radeon
);
180 /* ================================================================
183 static void r200Clear( GLcontext
*ctx
, GLbitfield mask
)
185 r200ContextPtr rmesa
= R200_CONTEXT(ctx
);
186 __DRIdrawable
*dPriv
= radeon_get_drawable(&rmesa
->radeon
);
188 GLuint color_mask
= 0;
189 GLuint orig_mask
= mask
;
191 if ( R200_DEBUG
& RADEON_IOCTL
) {
192 if (rmesa
->radeon
.sarea
)
193 fprintf( stderr
, "r200Clear %x %d\n", mask
, rmesa
->radeon
.sarea
->pfCurrentPage
);
195 fprintf( stderr
, "r200Clear %x radeon->sarea is NULL\n", mask
);
199 LOCK_HARDWARE( &rmesa
->radeon
);
200 UNLOCK_HARDWARE( &rmesa
->radeon
);
201 if ( dPriv
->numClipRects
== 0 )
207 if ( mask
& BUFFER_BIT_FRONT_LEFT
) {
208 flags
|= RADEON_FRONT
;
209 color_mask
= rmesa
->hw
.msk
.cmd
[MSK_RB3D_PLANEMASK
];
210 mask
&= ~BUFFER_BIT_FRONT_LEFT
;
213 if ( mask
& BUFFER_BIT_BACK_LEFT
) {
214 flags
|= RADEON_BACK
;
215 color_mask
= rmesa
->hw
.msk
.cmd
[MSK_RB3D_PLANEMASK
];
216 mask
&= ~BUFFER_BIT_BACK_LEFT
;
219 if ( mask
& BUFFER_BIT_DEPTH
) {
220 flags
|= RADEON_DEPTH
;
221 mask
&= ~BUFFER_BIT_DEPTH
;
224 if ( (mask
& BUFFER_BIT_STENCIL
) ) {
225 flags
|= RADEON_STENCIL
;
226 mask
&= ~BUFFER_BIT_STENCIL
;
230 if (R200_DEBUG
& RADEON_FALLBACKS
)
231 fprintf(stderr
, "%s: swrast clear, mask: %x\n", __FUNCTION__
, mask
);
232 _swrast_Clear( ctx
, mask
);
238 if (rmesa
->using_hyperz
) {
239 flags
|= RADEON_USE_COMP_ZBUF
;
240 /* if (rmesa->radeon.radeonScreen->chip_family == CHIP_FAMILY_R200)
241 flags |= RADEON_USE_HIERZ; */
242 if (!((flags
& RADEON_DEPTH
) && (flags
& RADEON_STENCIL
) &&
243 ((rmesa
->radeon
.state
.stencil
.clear
& R200_STENCIL_WRITE_MASK
) == R200_STENCIL_WRITE_MASK
))) {
244 flags
|= RADEON_CLEAR_FASTZ
;
248 if (rmesa
->radeon
.radeonScreen
->kernel_mm
)
249 radeonUserClear(ctx
, orig_mask
);
251 r200KernelClear(ctx
, flags
);
252 rmesa
->radeon
.hw
.all_dirty
= GL_TRUE
;
256 /* This version of AllocateMemoryMESA allocates only GART memory, and
257 * only does so after the point at which the driver has been
260 * Theoretically a valid context isn't required. However, in this
261 * implementation, it is, as I'm using the hardware lock to protect
262 * the kernel data structures, and the current context to get the
265 void *r200AllocateMemoryMESA(__DRIscreen
*screen
, GLsizei size
,
266 GLfloat readfreq
, GLfloat writefreq
,
269 GET_CURRENT_CONTEXT(ctx
);
270 r200ContextPtr rmesa
;
272 drm_radeon_mem_alloc_t alloc
;
275 if (R200_DEBUG
& RADEON_IOCTL
)
276 fprintf(stderr
, "%s sz %d %f/%f/%f\n", __FUNCTION__
, size
, readfreq
,
277 writefreq
, priority
);
279 if (!ctx
|| !(rmesa
= R200_CONTEXT(ctx
)) || !rmesa
->radeon
.radeonScreen
->gartTextures
.map
)
282 if (getenv("R200_NO_ALLOC"))
285 alloc
.region
= RADEON_MEM_REGION_GART
;
288 alloc
.region_offset
= ®ion_offset
;
290 ret
= drmCommandWriteRead( rmesa
->radeon
.radeonScreen
->driScreen
->fd
,
292 &alloc
, sizeof(alloc
));
295 fprintf(stderr
, "%s: DRM_RADEON_ALLOC ret %d\n", __FUNCTION__
, ret
);
300 char *region_start
= (char *)rmesa
->radeon
.radeonScreen
->gartTextures
.map
;
301 return (void *)(region_start
+ region_offset
);
306 /* Called via glXFreeMemoryMESA() */
307 void r200FreeMemoryMESA(__DRIscreen
*screen
, GLvoid
*pointer
)
309 GET_CURRENT_CONTEXT(ctx
);
310 r200ContextPtr rmesa
;
311 ptrdiff_t region_offset
;
312 drm_radeon_mem_free_t memfree
;
315 if (R200_DEBUG
& RADEON_IOCTL
)
316 fprintf(stderr
, "%s %p\n", __FUNCTION__
, pointer
);
318 if (!ctx
|| !(rmesa
= R200_CONTEXT(ctx
)) || !rmesa
->radeon
.radeonScreen
->gartTextures
.map
) {
319 fprintf(stderr
, "%s: no context\n", __FUNCTION__
);
323 region_offset
= (char *)pointer
- (char *)rmesa
->radeon
.radeonScreen
->gartTextures
.map
;
325 if (region_offset
< 0 ||
326 region_offset
> rmesa
->radeon
.radeonScreen
->gartTextures
.size
) {
327 fprintf(stderr
, "offset %d outside range 0..%d\n", region_offset
,
328 rmesa
->radeon
.radeonScreen
->gartTextures
.size
);
332 memfree
.region
= RADEON_MEM_REGION_GART
;
333 memfree
.region_offset
= region_offset
;
335 ret
= drmCommandWrite( rmesa
->radeon
.radeonScreen
->driScreen
->fd
,
337 &memfree
, sizeof(memfree
));
340 fprintf(stderr
, "%s: DRM_RADEON_FREE ret %d\n", __FUNCTION__
, ret
);
343 /* Called via glXGetMemoryOffsetMESA() */
344 GLuint
r200GetMemoryOffsetMESA(__DRIscreen
*screen
, const GLvoid
*pointer
)
346 GET_CURRENT_CONTEXT(ctx
);
347 r200ContextPtr rmesa
;
350 if (!ctx
|| !(rmesa
= R200_CONTEXT(ctx
)) ) {
351 fprintf(stderr
, "%s: no context\n", __FUNCTION__
);
355 if (!r200IsGartMemory( rmesa
, pointer
, 0 ))
358 card_offset
= r200GartOffsetFromVirtual( rmesa
, pointer
);
360 return card_offset
- rmesa
->radeon
.radeonScreen
->gart_base
;
363 GLboolean
r200IsGartMemory( r200ContextPtr rmesa
, const GLvoid
*pointer
,
366 ptrdiff_t offset
= (char *)pointer
- (char *)rmesa
->radeon
.radeonScreen
->gartTextures
.map
;
367 int valid
= (size
>= 0 &&
369 offset
+ size
< rmesa
->radeon
.radeonScreen
->gartTextures
.size
);
371 if (R200_DEBUG
& RADEON_IOCTL
)
372 fprintf(stderr
, "r200IsGartMemory( %p ) : %d\n", pointer
, valid
);
378 GLuint
r200GartOffsetFromVirtual( r200ContextPtr rmesa
, const GLvoid
*pointer
)
380 ptrdiff_t offset
= (char *)pointer
- (char *)rmesa
->radeon
.radeonScreen
->gartTextures
.map
;
382 if (offset
< 0 || offset
> rmesa
->radeon
.radeonScreen
->gartTextures
.size
)
385 return rmesa
->radeon
.radeonScreen
->gart_texture_offset
+ offset
;
390 void r200InitIoctlFuncs( struct dd_function_table
*functions
)
392 functions
->Clear
= r200Clear
;
393 functions
->Finish
= radeonFinish
;
394 functions
->Flush
= radeonFlush
;