Merge remote branch 'origin/master' into nv50-compiler
[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
28 /*
29 * Authors:
30 * Sung-Ching Lin <sclin@sis.com.tw>
31 * Eric Anholt <anholt@FreeBSD.org>
32 */
33
34 #include "sis_context.h"
35 #include "sis_alloc.h"
36
37 #include "sis_common.h"
38
39 #include <unistd.h>
40
41 #define Z_BUFFER_HW_ALIGNMENT 16
42 #define Z_BUFFER_HW_PLUS (16 + 4)
43
44 /* 3D engine uses 2, and bitblt uses 4 */
45 #define DRAW_BUFFER_HW_ALIGNMENT 16
46 #define DRAW_BUFFER_HW_PLUS (16 + 4)
47
48 #define ALIGNMENT(value, align) (((value) + (align) - 1) / (align) * (align))
49
50 static int _total_video_memory_used = 0;
51 static int _total_video_memory_count = 0;
52
53 void *
54 sisAllocFB( sisContextPtr smesa, GLuint size, void **handle )
55 {
56 drm_sis_mem_t fb;
57
58 _total_video_memory_used += size;
59
60 fb.context = smesa->hHWContext;
61 fb.size = size;
62 if (drmCommandWriteRead( smesa->driFd, DRM_SIS_FB_ALLOC, &fb,
63 sizeof(drm_sis_mem_t) ) || fb.offset == 0)
64 {
65 return NULL;
66 }
67 *handle = (void *)fb.free;
68
69 if (SIS_VERBOSE & VERBOSE_SIS_MEMORY) {
70 fprintf(stderr, "sisAllocFB: size=%d, offset=%lu, pid=%d, count=%d\n",
71 size, fb.offset, (GLint)getpid(),
72 ++_total_video_memory_count);
73 }
74
75 return (void *)(smesa->FbBase + fb.offset);
76 }
77
78 void
79 sisFreeFB( sisContextPtr smesa, void *handle )
80 {
81 drm_sis_mem_t fb;
82
83 if (SIS_VERBOSE & VERBOSE_SIS_MEMORY) {
84 fprintf(stderr, "sisFreeFB: free=%p, pid=%d, count=%d\n",
85 handle, (GLint)getpid(), --_total_video_memory_count);
86 }
87
88 fb.context = smesa->hHWContext;
89 fb.free = handle;
90 drmCommandWrite( smesa->driFd, DRM_SIS_FB_FREE, &fb, sizeof(drm_sis_mem_t) );
91 }
92
93 void *
94 sisAllocAGP( sisContextPtr smesa, GLuint size, void **handle )
95 {
96 drm_sis_mem_t agp;
97
98 if (smesa->AGPSize == 0)
99 return NULL;
100
101 agp.context = smesa->hHWContext;
102 agp.size = size;
103 if (drmCommandWriteRead( smesa->driFd, DRM_SIS_AGP_ALLOC, &agp,
104 sizeof(drm_sis_mem_t) ) || agp.offset == 0)
105 {
106 return NULL;
107 }
108 *handle = (void *)agp.free;
109
110 if (SIS_VERBOSE & VERBOSE_SIS_MEMORY) {
111 fprintf(stderr, "sisAllocAGP: size=%u, offset=%lu, pid=%d, count=%d\n",
112 size, agp.offset, (GLint)getpid(),
113 ++_total_video_memory_count);
114 }
115
116 return (void *)(smesa->AGPBase + agp.offset);
117 }
118
119 void
120 sisFreeAGP( sisContextPtr smesa, void *handle )
121 {
122 drm_sis_mem_t agp;
123
124 if (SIS_VERBOSE & VERBOSE_SIS_MEMORY) {
125 fprintf(stderr, "sisFreeAGP: free=%p, pid=%d, count=%d\n",
126 handle, (GLint)getpid(), --_total_video_memory_count);
127 }
128
129 agp.context = smesa->hHWContext;
130 agp.free = handle;
131 drmCommandWrite( smesa->driFd, DRM_SIS_AGP_FREE, &agp,
132 sizeof(drm_sis_mem_t) );
133 }
134
135 void
136 sisAllocZStencilBuffer( sisContextPtr smesa )
137 {
138 int cpp = ( smesa->glCtx->Visual.depthBits +
139 smesa->glCtx->Visual.stencilBits ) / 8;
140 char *addr;
141
142 smesa->depth.bpp = cpp * 8;
143 smesa->depth.pitch = ALIGNMENT(smesa->driDrawable->w * cpp, 4);
144 smesa->depth.size = smesa->depth.pitch * smesa->driDrawable->h;
145 smesa->depth.size += Z_BUFFER_HW_PLUS;
146
147 addr = sisAllocFB(smesa, smesa->depth.size, &smesa->depth.handle);
148 if (addr == NULL)
149 sis_fatal_error("Failure to allocate Z buffer.\n");
150 addr = (char *)ALIGNMENT((unsigned long)addr, Z_BUFFER_HW_ALIGNMENT);
151
152 smesa->depth.map = addr;
153 smesa->depth.offset = addr - (char *)smesa->FbBase;
154
155 /* stencil buffer is same as depth buffer */
156 smesa->stencil.size = smesa->depth.size;
157 smesa->stencil.offset = smesa->depth.offset;
158 smesa->stencil.handle = smesa->depth.handle;
159 smesa->stencil.pitch = smesa->depth.pitch;
160 smesa->stencil.bpp = smesa->depth.bpp;
161 smesa->stencil.map = smesa->depth.map;
162 }
163
164 void
165 sisFreeZStencilBuffer( sisContextPtr smesa )
166 {
167 sisFreeFB(smesa, smesa->depth.handle);
168 smesa->depth.map = NULL;
169 smesa->depth.offset = 0;
170 }
171
172 void
173 sisAllocBackbuffer( sisContextPtr smesa )
174 {
175 int cpp = smesa->bytesPerPixel;
176 char *addr;
177
178 smesa->back.bpp = smesa->bytesPerPixel * 8;
179 smesa->back.pitch = ALIGNMENT(smesa->driDrawable->w * cpp, 4);
180 smesa->back.size = smesa->back.pitch * smesa->driDrawable->h;
181 smesa->back.size += DRAW_BUFFER_HW_PLUS;
182
183 addr = sisAllocFB(smesa, smesa->back.size, &smesa->back.handle);
184 if (addr == NULL)
185 sis_fatal_error("Failure to allocate back buffer.\n");
186 addr = (char *)ALIGNMENT((unsigned long)addr, DRAW_BUFFER_HW_ALIGNMENT);
187
188 smesa->back.map = addr;
189 smesa->back.offset = addr - (char *)smesa->FbBase;
190 }
191
192 void
193 sisFreeBackbuffer( sisContextPtr smesa )
194 {
195 sisFreeFB(smesa, smesa->back.handle);
196 smesa->back.map = NULL;
197 smesa->back.offset = 0;
198 }