summaryrefslogtreecommitdiff
path: root/src/mesa/drivers/dri/sis/sis_alloc.c
blob: aa71f269d1497bed65283a9f819e49a0a890ccde (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/**************************************************************************

Copyright 2000 Silicon Integrated Systems Corp, Inc., HsinChu, Taiwan.
Copyright 2003 Eric Anholt
All Rights Reserved.

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
on the rights to use, copy, modify, merge, publish, distribute, sub
license, and/or sell copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice (including the next
paragraph) shall be included in all copies or substantial portions of the
Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
ATI, PRECISION INSIGHT AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE.

**************************************************************************/
/* $XFree86: xc/lib/GL/mesa/src/drv/sis/sis_alloc.c,v 1.7 2001/01/08 01:07:29 martin Exp $ */

/*
 * Authors:
 *   Sung-Ching Lin <sclin@sis.com.tw>
 *   Eric Anholt <anholt@FreeBSD.org>
 */

#include <assert.h>

#include "sis_context.h"
#include "sis_alloc.h"

#include "sis_common.h"

#define Z_BUFFER_HW_ALIGNMENT 16
#define Z_BUFFER_HW_PLUS (16 + 4)

/* 3D engine uses 2, and bitblt uses 4 */
#define DRAW_BUFFER_HW_ALIGNMENT 16
#define DRAW_BUFFER_HW_PLUS (16 + 4)

#ifdef ROUNDUP
#undef ROUNDUP
#endif
#define ROUNDUP(nbytes, pad) (((nbytes)+(pad-1))/(pad))

#ifdef ALIGNMENT
#undef ALIGNMENT
#endif
#define ALIGNMENT(value, align) (ROUNDUP((value),(align))*(align))

static int _total_video_memory_used = 0;
static int _total_video_memory_count = 0;

void *
sisAllocFB( sisContextPtr smesa, GLuint size, void **handle )
{
   drm_sis_mem_t fb;

   _total_video_memory_used += size;

   fb.context = smesa->hHWContext;
   fb.size = size;
   if (drmCommandWriteRead( smesa->driFd, DRM_SIS_FB_ALLOC, &fb, 
      sizeof(drm_sis_mem_t) ) || fb.offset == 0)
   {
      return NULL;
   }
   *handle = (void *)fb.free;

   if (SIS_VERBOSE & VERBOSE_SIS_MEMORY) {
      fprintf(stderr, "sisAllocFB: size=%d, offset=%lu, pid=%d, count=%d\n", 
              size, fb.offset, (GLint)getpid(), 
              ++_total_video_memory_count);
   }

   return (void *)(smesa->FbBase + fb.offset);
}

void
sisFreeFB( sisContextPtr smesa, void *handle )
{
   drm_sis_mem_t fb;

   if (SIS_VERBOSE & VERBOSE_SIS_MEMORY) {
      fprintf(stderr, "sisFreeFB: free=%p, pid=%d, count=%d\n", 
              handle, (GLint)getpid(), --_total_video_memory_count);
   }

   fb.context = smesa->hHWContext;
   fb.free = handle;
   drmCommandWrite( smesa->driFd, DRM_SIS_FB_FREE, &fb, sizeof(drm_sis_mem_t) );
}

void *
sisAllocAGP( sisContextPtr smesa, GLuint size, void **handle )
{
   drm_sis_mem_t agp;
   
   if (smesa->AGPSize == 0)
      return NULL;

   agp.context = smesa->hHWContext;
   agp.size = size;
   if (drmCommandWriteRead( smesa->driFd, DRM_SIS_AGP_ALLOC, &agp,
      sizeof(drm_sis_mem_t) ) || agp.offset == 0)
   {
      return NULL;
   }
   *handle = (void *)agp.free;

   if (SIS_VERBOSE & VERBOSE_SIS_MEMORY) {
      fprintf(stderr, "sisAllocAGP: size=%u, offset=%lu, pid=%d, count=%d\n", 
              size, agp.offset, (GLint)getpid(), 
              ++_total_video_memory_count);
   }

   return (void *)(smesa->AGPBase + agp.offset);
}

void
sisFreeAGP( sisContextPtr smesa, void *handle )
{
   drm_sis_mem_t agp;

   if (SIS_VERBOSE & VERBOSE_SIS_MEMORY) {
      fprintf(stderr, "sisFreeAGP: free=%p, pid=%d, count=%d\n", 
              handle, (GLint)getpid(), --_total_video_memory_count);
   }
  
   agp.context = smesa->hHWContext;
   agp.free = handle;
   drmCommandWrite( smesa->driFd, DRM_SIS_AGP_FREE, &agp,
      sizeof(drm_sis_mem_t) );
}

void
sisAllocZStencilBuffer( sisContextPtr smesa )
{
   GLuint z_depth;
   GLuint totalBytes;
   int width2;

   GLubyte *addr;

   z_depth = ( smesa->glCtx->Visual.depthBits +
               smesa->glCtx->Visual.stencilBits ) / 8;

   width2 = ALIGNMENT( smesa->width * z_depth, 4 );

   totalBytes = smesa->height * width2 + Z_BUFFER_HW_PLUS;

   addr = sisAllocFB( smesa, totalBytes, &smesa->zbFree );
   if (addr == NULL)
   {
      fprintf (stderr, "SIS driver : out of video memory\n");
      sis_fatal_error ();
   }

   if (SIS_VERBOSE & VERBOSE_SIS_BUFFER) {
      fprintf(stderr, "sis_alloc_z_stencil_buffer: addr=%p\n", addr);
   }

   addr = (GLubyte *)ALIGNMENT( (unsigned long)addr, Z_BUFFER_HW_ALIGNMENT );

   smesa->depthbuffer = (void *) addr;
   smesa->depthPitch = width2;

   /* set pZClearPacket */
   memset( &smesa->zClearPacket, 0, sizeof(ENGPACKET) );

   smesa->zClearPacket.dwSrcPitch = (z_depth == 2) ? 0x80000000 : 0xf0000000;
   smesa->zClearPacket.dwDestBaseAddr = (GLint)(addr -
      (unsigned long)smesa->FbBase);
   smesa->zClearPacket.wDestPitch = width2;
   smesa->zClearPacket.stdwDestPos.wY = 0;
   smesa->zClearPacket.stdwDestPos.wX = 0;

   smesa->zClearPacket.wDestHeight = smesa->virtualY;
   smesa->zClearPacket.stdwDim.wWidth = (GLshort)width2 / z_depth;
   smesa->zClearPacket.stdwDim.wHeight = (GLshort)smesa->height;
   smesa->zClearPacket.stdwCmd.cRop = 0xf0;

   if (smesa->blockWrite)
      smesa->zClearPacket.stdwCmd.cCmd0 = CMD0_PAT_FG_COLOR;
   else
      smesa->zClearPacket.stdwCmd.cCmd0 = 0;
   smesa->zClearPacket.stdwCmd.cCmd1 = CMD1_DIR_X_INC | CMD1_DIR_Y_INC;
}

void
sisFreeZStencilBuffer( sisContextPtr smesa )
{
   sisFreeFB( smesa, smesa->zbFree );
   smesa->zbFree = NULL;
   smesa->depthbuffer = NULL;
}

void
sisAllocBackbuffer( sisContextPtr smesa )
{
   GLuint depth = smesa->bytesPerPixel;
   GLuint size, width2;

   char *addr;

   width2 = (depth == 2) ? ALIGNMENT (smesa->width, 2) : smesa->width;
   size = width2 * smesa->height * depth + DRAW_BUFFER_HW_PLUS;

   /* Fixme: unique context alloc/free back-buffer? */
   addr = sisAllocFB( smesa, size, &smesa->bbFree );
   if (addr == NULL)
   {
      fprintf (stderr, "SIS driver : out of video memory\n");
      sis_fatal_error ();
   }

   addr = (char *)ALIGNMENT( (unsigned long)addr, DRAW_BUFFER_HW_ALIGNMENT );

   smesa->backbuffer = addr;
   smesa->backOffset = (GLint)(addr - (unsigned long)smesa->FbBase);
   smesa->backPitch = width2 * depth;

   memset ( &smesa->cbClearPacket, 0, sizeof(ENGPACKET) );

   smesa->cbClearPacket.dwSrcPitch = (depth == 2) ? 0x80000000 : 0xf0000000;
   smesa->cbClearPacket.dwDestBaseAddr = smesa->backOffset;
   smesa->cbClearPacket.wDestPitch = smesa->backPitch;
   smesa->cbClearPacket.stdwDestPos.wY = 0;
   smesa->cbClearPacket.stdwDestPos.wX = 0;

   smesa->cbClearPacket.wDestHeight = smesa->virtualY;
   smesa->cbClearPacket.stdwDim.wWidth = (GLshort) width2;
   smesa->cbClearPacket.stdwDim.wHeight = (GLshort) smesa->height;
   smesa->cbClearPacket.stdwCmd.cRop = 0xf0;

   if (smesa->blockWrite)
      smesa->cbClearPacket.stdwCmd.cCmd0 = (GLbyte)(CMD0_PAT_FG_COLOR);
   else
      smesa->cbClearPacket.stdwCmd.cCmd0 = 0;
   smesa->cbClearPacket.stdwCmd.cCmd1 = CMD1_DIR_X_INC | CMD1_DIR_Y_INC;
}

void
sisFreeBackbuffer( sisContextPtr smesa )
{
   sisFreeFB( smesa, smesa->bbFree );
   smesa->backbuffer = NULL; 
}