1 /**************************************************************************
3 Copyright 2000 Silicon Integrated Systems Corp, Inc., HsinChu, Taiwan.
4 Copyright 2003 Eric Anholt
7 Permission is hereby granted, free of charge, to any person obtaining a
8 copy of this software and associated documentation files (the "Software"),
9 to deal in the Software without restriction, including without limitation
10 on the rights to use, copy, modify, merge, publish, distribute, sub
11 license, and/or sell copies of the Software, and to permit persons to whom
12 the Software is furnished to do so, subject to the following conditions:
14 The above copyright notice and this permission notice (including the next
15 paragraph) shall be included in all copies or substantial portions of the
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 ERIC ANHOLT OR SILICON INTEGRATED SYSTEMS CORP BE LIABLE FOR ANY CLAIM,
22 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
27 /* $XFree86: xc/lib/GL/mesa/src/drv/sis/sis_alloc.c,v 1.7 2001/01/08 01:07:29 martin Exp $ */
31 * Sung-Ching Lin <sclin@sis.com.tw>
32 * Eric Anholt <anholt@FreeBSD.org>
35 #include "sis_context.h"
36 #include "sis_alloc.h"
38 #include "sis_common.h"
42 #define Z_BUFFER_HW_ALIGNMENT 16
43 #define Z_BUFFER_HW_PLUS (16 + 4)
45 /* 3D engine uses 2, and bitblt uses 4 */
46 #define DRAW_BUFFER_HW_ALIGNMENT 16
47 #define DRAW_BUFFER_HW_PLUS (16 + 4)
49 #define ALIGNMENT(value, align) (((value) + (align) - 1) / (align) * (align))
51 static int _total_video_memory_used
= 0;
52 static int _total_video_memory_count
= 0;
55 sisAllocFB( sisContextPtr smesa
, GLuint size
, void **handle
)
59 _total_video_memory_used
+= size
;
61 fb
.context
= smesa
->hHWContext
;
63 if (drmCommandWriteRead( smesa
->driFd
, DRM_SIS_FB_ALLOC
, &fb
,
64 sizeof(drm_sis_mem_t
) ) || fb
.offset
== 0)
68 *handle
= (void *)fb
.free
;
70 if (SIS_VERBOSE
& VERBOSE_SIS_MEMORY
) {
71 fprintf(stderr
, "sisAllocFB: size=%d, offset=%lu, pid=%d, count=%d\n",
72 size
, fb
.offset
, (GLint
)getpid(),
73 ++_total_video_memory_count
);
76 return (void *)(smesa
->FbBase
+ fb
.offset
);
80 sisFreeFB( sisContextPtr smesa
, void *handle
)
84 if (SIS_VERBOSE
& VERBOSE_SIS_MEMORY
) {
85 fprintf(stderr
, "sisFreeFB: free=%p, pid=%d, count=%d\n",
86 handle
, (GLint
)getpid(), --_total_video_memory_count
);
89 fb
.context
= smesa
->hHWContext
;
91 drmCommandWrite( smesa
->driFd
, DRM_SIS_FB_FREE
, &fb
, sizeof(drm_sis_mem_t
) );
95 sisAllocAGP( sisContextPtr smesa
, GLuint size
, void **handle
)
99 if (smesa
->AGPSize
== 0)
102 agp
.context
= smesa
->hHWContext
;
104 if (drmCommandWriteRead( smesa
->driFd
, DRM_SIS_AGP_ALLOC
, &agp
,
105 sizeof(drm_sis_mem_t
) ) || agp
.offset
== 0)
109 *handle
= (void *)agp
.free
;
111 if (SIS_VERBOSE
& VERBOSE_SIS_MEMORY
) {
112 fprintf(stderr
, "sisAllocAGP: size=%u, offset=%lu, pid=%d, count=%d\n",
113 size
, agp
.offset
, (GLint
)getpid(),
114 ++_total_video_memory_count
);
117 return (void *)(smesa
->AGPBase
+ agp
.offset
);
121 sisFreeAGP( sisContextPtr smesa
, void *handle
)
125 if (SIS_VERBOSE
& VERBOSE_SIS_MEMORY
) {
126 fprintf(stderr
, "sisFreeAGP: free=%p, pid=%d, count=%d\n",
127 handle
, (GLint
)getpid(), --_total_video_memory_count
);
130 agp
.context
= smesa
->hHWContext
;
132 drmCommandWrite( smesa
->driFd
, DRM_SIS_AGP_FREE
, &agp
,
133 sizeof(drm_sis_mem_t
) );
137 sisAllocZStencilBuffer( sisContextPtr smesa
)
139 int cpp
= ( smesa
->glCtx
->Visual
.depthBits
+
140 smesa
->glCtx
->Visual
.stencilBits
) / 8;
143 smesa
->depth
.bpp
= cpp
* 8;
144 smesa
->depth
.pitch
= ALIGNMENT(smesa
->driDrawable
->w
* cpp
, 4);
145 smesa
->depth
.size
= smesa
->depth
.pitch
* smesa
->driDrawable
->h
;
146 smesa
->depth
.size
+= Z_BUFFER_HW_PLUS
;
148 addr
= sisAllocFB(smesa
, smesa
->depth
.size
, &smesa
->depth
.handle
);
150 sis_fatal_error("Failure to allocate Z buffer.\n");
151 addr
= (char *)ALIGNMENT((unsigned long)addr
, Z_BUFFER_HW_ALIGNMENT
);
153 smesa
->depth
.map
= addr
;
154 smesa
->depth
.offset
= addr
- smesa
->FbBase
;
156 /* stencil buffer is same as depth buffer */
157 smesa
->stencil
.size
= smesa
->depth
.size
;
158 smesa
->stencil
.offset
= smesa
->depth
.offset
;
159 smesa
->stencil
.handle
= smesa
->depth
.handle
;
160 smesa
->stencil
.pitch
= smesa
->depth
.pitch
;
161 smesa
->stencil
.bpp
= smesa
->depth
.bpp
;
162 smesa
->stencil
.map
= smesa
->depth
.map
;
166 sisFreeZStencilBuffer( sisContextPtr smesa
)
168 sisFreeFB(smesa
, smesa
->depth
.handle
);
169 smesa
->depth
.map
= NULL
;
170 smesa
->depth
.offset
= 0;
174 sisAllocBackbuffer( sisContextPtr smesa
)
176 int cpp
= smesa
->bytesPerPixel
;
179 smesa
->back
.bpp
= smesa
->bytesPerPixel
* 8;
180 smesa
->back
.pitch
= ALIGNMENT(smesa
->driDrawable
->w
* cpp
, 4);
181 smesa
->back
.size
= smesa
->back
.pitch
* smesa
->driDrawable
->h
;
182 smesa
->back
.size
+= DRAW_BUFFER_HW_PLUS
;
184 addr
= sisAllocFB(smesa
, smesa
->back
.size
, &smesa
->back
.handle
);
186 sis_fatal_error("Failure to allocate back buffer.\n");
187 addr
= (char *)ALIGNMENT((unsigned long)addr
, DRAW_BUFFER_HW_ALIGNMENT
);
189 smesa
->back
.map
= addr
;
190 smesa
->back
.offset
= addr
- smesa
->FbBase
;
194 sisFreeBackbuffer( sisContextPtr smesa
)
196 sisFreeFB(smesa
, smesa
->back
.handle
);
197 smesa
->back
.map
= NULL
;
198 smesa
->back
.offset
= 0;