bring over build fixes from stable branch
[mesa.git] / src / mesa / drivers / dri / sis / sis_alloc.c
1 /**************************************************************************
2
3 Copyright 2000 Silicon Integrated Systems Corp, Inc., HsinChu, Taiwan.
4 Copyright 2003 Eric Anholt
5 All Rights Reserved.
6
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:
13
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
16 Software.
17
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.
25
26 **************************************************************************/
27 /* $XFree86: xc/lib/GL/mesa/src/drv/sis/sis_alloc.c,v 1.7 2001/01/08 01:07:29 martin Exp $ */
28
29 /*
30 * Authors:
31 * Sung-Ching Lin <sclin@sis.com.tw>
32 * Eric Anholt <anholt@FreeBSD.org>
33 */
34
35 #include "sis_context.h"
36 #include "sis_alloc.h"
37
38 #include "sis_common.h"
39
40 #include <unistd.h>
41
42 #define Z_BUFFER_HW_ALIGNMENT 16
43 #define Z_BUFFER_HW_PLUS (16 + 4)
44
45 /* 3D engine uses 2, and bitblt uses 4 */
46 #define DRAW_BUFFER_HW_ALIGNMENT 16
47 #define DRAW_BUFFER_HW_PLUS (16 + 4)
48
49 #define ALIGNMENT(value, align) (((value) + (align) - 1) / (align) * (align))
50
51 static int _total_video_memory_used = 0;
52 static int _total_video_memory_count = 0;
53
54 void *
55 sisAllocFB( sisContextPtr smesa, GLuint size, void **handle )
56 {
57 drm_sis_mem_t fb;
58
59 _total_video_memory_used += size;
60
61 fb.context = smesa->hHWContext;
62 fb.size = size;
63 if (drmCommandWriteRead( smesa->driFd, DRM_SIS_FB_ALLOC, &fb,
64 sizeof(drm_sis_mem_t) ) || fb.offset == 0)
65 {
66 return NULL;
67 }
68 *handle = (void *)fb.free;
69
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);
74 }
75
76 return (void *)(smesa->FbBase + fb.offset);
77 }
78
79 void
80 sisFreeFB( sisContextPtr smesa, void *handle )
81 {
82 drm_sis_mem_t fb;
83
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);
87 }
88
89 fb.context = smesa->hHWContext;
90 fb.free = handle;
91 drmCommandWrite( smesa->driFd, DRM_SIS_FB_FREE, &fb, sizeof(drm_sis_mem_t) );
92 }
93
94 void *
95 sisAllocAGP( sisContextPtr smesa, GLuint size, void **handle )
96 {
97 drm_sis_mem_t agp;
98
99 if (smesa->AGPSize == 0)
100 return NULL;
101
102 agp.context = smesa->hHWContext;
103 agp.size = size;
104 if (drmCommandWriteRead( smesa->driFd, DRM_SIS_AGP_ALLOC, &agp,
105 sizeof(drm_sis_mem_t) ) || agp.offset == 0)
106 {
107 return NULL;
108 }
109 *handle = (void *)agp.free;
110
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);
115 }
116
117 return (void *)(smesa->AGPBase + agp.offset);
118 }
119
120 void
121 sisFreeAGP( sisContextPtr smesa, void *handle )
122 {
123 drm_sis_mem_t agp;
124
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);
128 }
129
130 agp.context = smesa->hHWContext;
131 agp.free = handle;
132 drmCommandWrite( smesa->driFd, DRM_SIS_AGP_FREE, &agp,
133 sizeof(drm_sis_mem_t) );
134 }
135
136 void
137 sisAllocZStencilBuffer( sisContextPtr smesa )
138 {
139 GLuint z_depth;
140 GLuint totalBytes;
141 int width2;
142
143 GLubyte *addr;
144
145 z_depth = ( smesa->glCtx->Visual.depthBits +
146 smesa->glCtx->Visual.stencilBits ) / 8;
147
148 width2 = ALIGNMENT( smesa->width * z_depth, 4 );
149
150 totalBytes = smesa->height * width2 + Z_BUFFER_HW_PLUS;
151
152 addr = sisAllocFB( smesa, totalBytes, &smesa->zbFree );
153 if (addr == NULL)
154 {
155 fprintf (stderr, "SIS driver : out of video memory\n");
156 sis_fatal_error ();
157 }
158
159 if (SIS_VERBOSE & VERBOSE_SIS_BUFFER) {
160 fprintf(stderr, "sis_alloc_z_stencil_buffer: addr=%p\n", addr);
161 }
162
163 addr = (GLubyte *)ALIGNMENT( (unsigned long)addr, Z_BUFFER_HW_ALIGNMENT );
164
165 smesa->depthbuffer = (void *) addr;
166 smesa->depthPitch = width2;
167 smesa->depthOffset = (unsigned long)addr - (unsigned long)smesa->FbBase;
168
169 /* set pZClearPacket */
170 memset( &smesa->zClearPacket, 0, sizeof(ENGPACKET) );
171
172 smesa->zClearPacket.dwSrcPitch = (z_depth == 2) ? 0x80000000 : 0xf0000000;
173 smesa->zClearPacket.dwDestBaseAddr = (unsigned long)(addr -
174 (unsigned long)smesa->FbBase);
175 smesa->zClearPacket.wDestPitch = width2;
176 smesa->zClearPacket.stdwDestPos.wY = 0;
177 smesa->zClearPacket.stdwDestPos.wX = 0;
178
179 smesa->zClearPacket.wDestHeight = smesa->virtualY;
180 smesa->zClearPacket.stdwDim.wWidth = (GLshort)width2 / z_depth;
181 smesa->zClearPacket.stdwDim.wHeight = (GLshort)smesa->height;
182 smesa->zClearPacket.stdwCmd.cRop = 0xf0;
183
184 if (smesa->blockWrite)
185 smesa->zClearPacket.stdwCmd.cCmd0 = CMD0_PAT_FG_COLOR;
186 else
187 smesa->zClearPacket.stdwCmd.cCmd0 = 0;
188 smesa->zClearPacket.stdwCmd.cCmd1 = CMD1_DIR_X_INC | CMD1_DIR_Y_INC;
189 }
190
191 void
192 sisFreeZStencilBuffer( sisContextPtr smesa )
193 {
194 sisFreeFB( smesa, smesa->zbFree );
195 smesa->zbFree = NULL;
196 smesa->depthbuffer = NULL;
197 }
198
199 void
200 sisAllocBackbuffer( sisContextPtr smesa )
201 {
202 GLuint depth = smesa->bytesPerPixel;
203 GLuint size, width2;
204
205 char *addr;
206
207 width2 = (depth == 2) ? ALIGNMENT (smesa->width, 2) : smesa->width;
208 size = width2 * smesa->height * depth + DRAW_BUFFER_HW_PLUS;
209
210 /* Fixme: unique context alloc/free back-buffer? */
211 addr = sisAllocFB( smesa, size, &smesa->bbFree );
212 if (addr == NULL)
213 {
214 fprintf (stderr, "SIS driver : out of video memory\n");
215 sis_fatal_error ();
216 }
217
218 addr = (char *)ALIGNMENT( (unsigned long)addr, DRAW_BUFFER_HW_ALIGNMENT );
219
220 smesa->backbuffer = addr;
221 smesa->backOffset = (unsigned long)(addr - (unsigned long)smesa->FbBase);
222 smesa->backPitch = width2 * depth;
223
224 memset ( &smesa->cbClearPacket, 0, sizeof(ENGPACKET) );
225
226 smesa->cbClearPacket.dwSrcPitch = (depth == 2) ? 0x80000000 : 0xf0000000;
227 smesa->cbClearPacket.dwDestBaseAddr = smesa->backOffset;
228 smesa->cbClearPacket.wDestPitch = smesa->backPitch;
229 smesa->cbClearPacket.stdwDestPos.wY = 0;
230 smesa->cbClearPacket.stdwDestPos.wX = 0;
231
232 smesa->cbClearPacket.wDestHeight = smesa->virtualY;
233 smesa->cbClearPacket.stdwDim.wWidth = (GLshort) width2;
234 smesa->cbClearPacket.stdwDim.wHeight = (GLshort) smesa->height;
235 smesa->cbClearPacket.stdwCmd.cRop = 0xf0;
236
237 if (smesa->blockWrite)
238 smesa->cbClearPacket.stdwCmd.cCmd0 = (GLbyte)(CMD0_PAT_FG_COLOR);
239 else
240 smesa->cbClearPacket.stdwCmd.cCmd0 = 0;
241 smesa->cbClearPacket.stdwCmd.cCmd1 = CMD1_DIR_X_INC | CMD1_DIR_Y_INC;
242 }
243
244 void
245 sisFreeBackbuffer( sisContextPtr smesa )
246 {
247 sisFreeFB( smesa, smesa->bbFree );
248 smesa->backbuffer = NULL;
249 }