1
/**************************************************************************
3
Copyright 2000 Silicon Integrated Systems Corp, Inc., HsinChu, Taiwan.
6
Permission is hereby granted, free of charge, to any person obtaining a
7
copy of this software and associated documentation files (the
8
"Software"), to deal in the Software without restriction, including
9
without limitation the rights to use, copy, modify, merge, publish,
10
distribute, sub license, and/or sell copies of the Software, and to
11
permit persons to whom the Software is furnished to do so, subject to
12
the following conditions:
14
The above copyright notice and this permission notice (including the
15
next paragraph) shall be included in all copies or substantial portions
18
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21
IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
22
ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24
SOFTWARE OR THE 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>
40
#if defined(XFree86Server) && !defined(XF86DRI)
41
# include "xf86fbman.h"
43
# define CONFIG_DRM_SIS
45
# undef CONFIG_DRM_SIS
47
# include <sys/ioctl.h>
50
#define Z_BUFFER_HW_ALIGNMENT 16
51
#define Z_BUFFER_HW_PLUS (16 + 4)
53
/* 3D engine uses 2, and bitblt uses 4 */
54
#define DRAW_BUFFER_HW_ALIGNMENT 16
55
#define DRAW_BUFFER_HW_PLUS (16 + 4)
57
#define TEXTURE_HW_ALIGNMENT 4
58
#define TEXTURE_HW_PLUS (4 + 4)
63
#define ROUNDUP(nbytes, pad) (((nbytes)+(pad-1))/(pad))
68
#define ALIGNMENT(value, align) (ROUNDUP((value),(align))*(align))
70
#if defined(XFree86Server) && !defined(XF86DRI)
73
sis_alloc_fb (__GLSiScontext * hwcx, GLuint size, void **free)
75
GLcontext *ctx = hwcx->gc;
76
XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
78
ScreenPtr pScreen = xmesa->display;
83
size = ROUNDUP (size, GET_DEPTH (hwcx));
84
*free = xf86AllocateLinearOffscreenArea (pScreen, size, 1,
90
pBox = &((FBAreaPtr) (*free))->box;
91
offset = pBox->y1 * GET_PITCH (hwcx) + pBox->x1 * GET_DEPTH (hwcx);
93
return GET_FbBase (hwcx) + offset;
97
sis_free_fb (int hHWContext, void *free)
99
xf86FreeOffscreenArea ((FBAreaPtr) free);
109
static int _total_video_memory_used = 0;
110
static int _total_video_memory_count = 0;
113
sis_alloc_fb (__GLSiScontext * hwcx, GLuint size, void **free)
115
GLcontext *ctx = hwcx->gc;
116
XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
120
_total_video_memory_used += size;
122
fb.context = xmesa->driContextPriv->hHWContext;
124
if(ioctl(hwcx->drmSubFD, SIS_IOCTL_FB_ALLOC, &fb) || !fb.offset)
126
*free = (void *)fb.free;
129
/* memset(fb.offset + GET_FbBase(hwcx), 0xff, size); */
131
if (SIS_VERBOSE&VERBOSE_SIS_MEMORY)
133
fprintf(stderr, "sis_alloc_fb: size=%u, offset=%lu, pid=%lu, count=%d\n",
134
size, (DWORD)fb.offset, (DWORD)getpid(),
135
++_total_video_memory_count);
138
return (void *)(fb.offset + GET_FbBase(hwcx));
142
sis_free_fb (int hHWContext, void *free)
146
if (SIS_VERBOSE&VERBOSE_SIS_MEMORY)
148
fprintf(stderr, "sis_free_fb: free=%lu, pid=%lu, count=%d\n",
149
(DWORD)free, (DWORD)getpid(), --_total_video_memory_count);
152
fb.context = hHWContext;
153
fb.free = (unsigned long)free;
154
ioctl(gDRMSubFD, SIS_IOCTL_FB_FREE, &fb);
160
sis_alloc_fb (__GLSiScontext * hwcx, GLuint size, void **free)
162
static char *vidmem_base = 0x400000;
163
char *rval = vidmem_base;
166
if(vidmem_base >= 31*0x100000)
169
*free = rval + (DWORD)hwcx->FbBase;
171
return rval + (DWORD)hwcx->FbBase;
175
sis_free_fb (int hHWContext, void *free)
185
sis_alloc_agp (__GLSiScontext * hwcx, GLuint size, void **free)
187
GLcontext *ctx = hwcx->gc;
188
XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
195
agp.context = xmesa->driContextPriv->hHWContext;
197
if(ioctl(hwcx->drmSubFD, SIS_IOCTL_AGP_ALLOC, &agp) || !agp.offset)
199
*free = (void *)agp.free;
201
if (SIS_VERBOSE&VERBOSE_SIS_MEMORY)
203
fprintf(stderr, "sis_alloc_agp: size=%u, offset=%lu, pid=%lu, count=%d\n",
204
size, (DWORD)agp.offset, (DWORD)getpid(),
205
++_total_video_memory_count);
208
return (void *)(agp.offset + GET_AGPBase(hwcx));
212
sis_free_agp (int hHWContext, void *free)
216
if (SIS_VERBOSE&VERBOSE_SIS_MEMORY)
218
fprintf(stderr, "sis_free_agp: free=%lu, pid=%lu, count=%d\n",
219
(DWORD)free, (DWORD)getpid(), --_total_video_memory_count);
222
agp.context = hHWContext;
223
agp.free = (unsigned long)free;
224
ioctl(gDRMSubFD, SIS_IOCTL_AGP_FREE, &agp);
228
static unsigned int Total_Real_Textures_Used = 0;
229
static unsigned int Total_Textures_Used = 0;
232
sis_alloc_z_stencil_buffer (GLcontext * ctx)
234
XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
235
__GLSiScontext *hwcx = (__GLSiScontext *) xmesa->private;
237
XMesaBuffer xm_buffer = xmesa->xm_buffer;
238
sisBufferInfo *priv = (sisBufferInfo *) xm_buffer->private;
246
z_depth = (ctx->Visual->DepthBits + ctx->Visual->StencilBits) / 8;
248
width2 = ALIGNMENT (xm_buffer->width * z_depth, 4);
250
totalBytes = xm_buffer->height * width2 + Z_BUFFER_HW_PLUS;
252
if (xm_buffer->depthbuffer)
254
sis_free_z_stencil_buffer (xm_buffer);
257
addr = sis_alloc_fb (hwcx, totalBytes, &priv->zbFree);
260
fprintf (stderr, "SIS driver : out of video memory\n");
264
if (SIS_VERBOSE&VERBOSE_SIS_BUFFER)
266
fprintf(stderr, "sis_alloc_z_stencil_buffer: addr=%lu\n", (DWORD)addr);
269
addr = (GLubyte *) ALIGNMENT ((unsigned long) addr, Z_BUFFER_HW_ALIGNMENT);
271
xm_buffer->depthbuffer = (void *) addr;
273
/* software render */
274
hwcx->swZBase = addr;
275
hwcx->swZPitch = width2;
277
/* set pZClearPacket */
278
memset (priv->pZClearPacket, 0, sizeof (ENGPACKET));
280
priv->pZClearPacket->dwSrcPitch = (z_depth == 2) ? 0x80000000 : 0xf0000000;
281
priv->pZClearPacket->dwDestBaseAddr =
282
(DWORD) addr - (DWORD) GET_FbBase (hwcx);
283
priv->pZClearPacket->wDestPitch = width2;
284
priv->pZClearPacket->stdwDestPos.wY = 0;
285
priv->pZClearPacket->stdwDestPos.wX = 0;
287
priv->pZClearPacket->wDestHeight = hwcx->virtualY;
288
priv->pZClearPacket->stdwDim.wWidth = (WORD) width2 / z_depth;
289
priv->pZClearPacket->stdwDim.wHeight = (WORD) xm_buffer->height;
290
priv->pZClearPacket->stdwCmd.cRop = 0xf0;
292
if (hwcx->blockWrite)
294
priv->pZClearPacket->stdwCmd.cCmd0 = (BYTE) (CMD0_PAT_FG_COLOR);
295
priv->pZClearPacket->stdwCmd.cCmd1 =
296
(BYTE) (CMD1_DIR_X_INC | CMD1_DIR_Y_INC);
300
priv->pZClearPacket->stdwCmd.cCmd0 = 0;
301
priv->pZClearPacket->stdwCmd.cCmd1 =
302
(BYTE) (CMD1_DIR_X_INC | CMD1_DIR_Y_INC);
307
sis_free_z_stencil_buffer (XMesaBuffer buf)
309
sisBufferInfo *priv = (sisBufferInfo *) buf->private;
310
XMesaContext xmesa = buf->xm_context;
312
sis_free_fb (xmesa->driContextPriv->hHWContext, priv->zbFree);
314
buf->depthbuffer = NULL;
318
sis_alloc_back_image (GLcontext * ctx, XMesaImage *image, void **free,
321
XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
322
__GLSiScontext *hwcx = (__GLSiScontext *) xmesa->private;
324
XMesaBuffer xm_buffer = xmesa->xm_buffer;
326
GLuint depth = GET_DEPTH (hwcx);
333
sis_free_back_image (xm_buffer, image, *free);
337
width2 = (depth == 2) ? ALIGNMENT (xm_buffer->width, 2) : xm_buffer->width;
338
size = width2 * xm_buffer->height * depth + DRAW_BUFFER_HW_PLUS;
340
/* Fixme: unique context alloc/free back-buffer? */
341
addr = sis_alloc_fb (hwcx, size, free);
344
fprintf (stderr, "SIS driver : out of video memory\n");
348
addr = (GLbyte *) ALIGNMENT ((unsigned long) addr, DRAW_BUFFER_HW_ALIGNMENT);
350
image->data = (char *)addr;
352
image->bytes_per_line = width2 * depth;
353
image->bits_per_pixel = depth * 8;
355
memset (packet, 0, sizeof (ENGPACKET));
357
packet->dwSrcPitch = (depth == 2) ? 0x80000000 : 0xf0000000;
358
packet->dwDestBaseAddr =
359
(DWORD) addr - (DWORD) GET_FbBase (hwcx);
360
packet->wDestPitch = image->bytes_per_line;
361
packet->stdwDestPos.wY = 0;
362
packet->stdwDestPos.wX = 0;
364
packet->wDestHeight = hwcx->virtualY;
365
packet->stdwDim.wWidth = (WORD) width2;
366
packet->stdwDim.wHeight = (WORD) xm_buffer->height;
367
packet->stdwCmd.cRop = 0xf0;
369
if (hwcx->blockWrite)
371
packet->stdwCmd.cCmd0 = (BYTE) (CMD0_PAT_FG_COLOR);
372
packet->stdwCmd.cCmd1 =
373
(BYTE) (CMD1_DIR_X_INC | CMD1_DIR_Y_INC);
377
packet->stdwCmd.cCmd0 = 0;
378
packet->stdwCmd.cCmd1 = (BYTE) (CMD1_DIR_X_INC | CMD1_DIR_Y_INC);
383
sis_free_back_image (XMesaBuffer buf, XMesaImage *image, void *free)
385
XMesaContext xmesa = buf->xm_context;
387
sis_free_fb (xmesa->driContextPriv->hHWContext, free);
392
sis_alloc_texture_image (GLcontext * ctx, GLtextureImage * image)
394
XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
395
__GLSiScontext *hwcx = (__GLSiScontext *) xmesa->private;
399
SIStextureArea *area = image->DriverData;
403
GLenum driver_format;
406
sis_free_texture_image (image);
408
area = calloc (1, sizeof (SIStextureArea));
410
fprintf (stderr, "SIS Driver : allocating context fails\n");
415
switch (image->IntFormat)
423
driver_format = GL_ALPHA8;
432
driver_format = GL_LUMINANCE8;
435
case GL_LUMINANCE_ALPHA:
436
case GL_LUMINANCE4_ALPHA4:
437
case GL_LUMINANCE6_ALPHA2:
438
case GL_LUMINANCE8_ALPHA8:
439
case GL_LUMINANCE12_ALPHA4:
440
case GL_LUMINANCE12_ALPHA12:
441
case GL_LUMINANCE16_ALPHA16:
443
driver_format = GL_LUMINANCE8_ALPHA8;
451
driver_format = GL_INTENSITY8;
463
driver_format = GL_RGB8;
475
driver_format = GL_RGBA8;
482
size = image->Width * image->Height * texel_size + TEXTURE_HW_PLUS;
485
addr = sis_alloc_fb (hwcx, size, &area->free);
486
area->memType = VIDEO_TYPE;
489
/* TODO: swap to agp memory*/
490
/* video memory allocation fails */
491
addr = sis_alloc_agp(hwcx, size, &area->free);
492
area->memType = AGP_TYPE;
495
/* TODO: swap to system memory */
500
fprintf (stderr, "SIS driver : out of video/agp memory\n");
506
(GLbyte *) ALIGNMENT ((unsigned long) addr, TEXTURE_HW_ALIGNMENT);
507
area->Pitch = image->Width * texel_size;
508
area->Format = driver_format;
509
area->Size = image->Width * image->Height * texel_size;
510
area->texelSize = texel_size;
511
area->hHWContext = xmesa->driContextPriv->hHWContext;
514
area->realSize = area->Size;
515
Total_Real_Textures_Used += area->realSize;
516
Total_Textures_Used++;
518
image->DriverData = area;
522
sis_free_texture_image (GLtextureImage * image)
524
SIStextureArea *area = (SIStextureArea *) image->DriverData;
527
Total_Real_Textures_Used -= area->realSize;
528
Total_Textures_Used--;
534
switch(area->memType){
536
sis_free_fb (area->hHWContext, area->free);
539
sis_free_agp (area->hHWContext, area->free);
546
image->DriverData = NULL;