Unichrome DRI:
[mesa.git] / src / mesa / drivers / dri / unichrome / via_fb.c
1 /*
2 * Copyright 1998-2003 VIA Technologies, Inc. All Rights Reserved.
3 * Copyright 2001-2003 S3 Graphics, Inc. All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sub license,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
14 * of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * VIA, S3 GRAPHICS, AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25 #include <assert.h>
26
27 #include "via_context.h"
28 #include "via_ioctl.h"
29 #include "via_fb.h"
30 #include "xf86drm.h"
31 #include <sys/ioctl.h>
32
33 GLboolean
34 via_alloc_back_buffer(viaContextPtr vmesa)
35 {
36 drm_via_mem_t fb;
37 unsigned char *pFB;
38 #ifdef DEBUG
39 if (VIA_DEBUG) fprintf(stderr, "%s - in\n", __FUNCTION__);
40 #endif
41 fb.context = vmesa->hHWContext;
42 fb.size = vmesa->back.size;
43 fb.type = VIDEO;
44 #ifdef DEBUG
45 if (VIA_DEBUG) fprintf(stderr, "context = %d, size =%d, type = %d\n", fb.context, fb.size, fb.type);
46 #endif
47 if (ioctl(vmesa->driFd, DRM_IOCTL_VIA_ALLOCMEM, &fb))
48 return GL_FALSE;
49
50 pFB = vmesa->driScreen->pFB;
51
52 vmesa->back.offset = fb.offset;
53 vmesa->back.map = (char *)(fb.offset + (GLuint)pFB);
54 vmesa->back.index = fb.index;
55 #ifdef DEBUG
56 if (VIA_DEBUG) {
57 fprintf(stderr, "back offset = %08x\n", vmesa->back.offset);
58 fprintf(stderr, "back index = %d\n", vmesa->back.index);
59 }
60
61 if (VIA_DEBUG) fprintf(stderr, "%s - out\n", __FUNCTION__);
62 #endif
63 return GL_TRUE;
64 }
65
66 GLboolean
67 via_alloc_front_buffer(viaContextPtr vmesa)
68 {
69 drm_via_mem_t fb;
70 unsigned char *pFB;
71 #ifdef DEBUG
72 if (VIA_DEBUG) fprintf(stderr, "%s - in\n", __FUNCTION__);
73 #endif
74 fb.context = vmesa->hHWContext;
75 fb.size = vmesa->back.size;
76 fb.type = VIDEO;
77 #ifdef DEBUG
78 if (VIA_DEBUG) fprintf(stderr, "context = %d, size =%d, type = %d\n", fb.context, fb.size, fb.type);
79 #endif
80 if (ioctl(vmesa->driFd, DRM_IOCTL_VIA_ALLOCMEM, &fb))
81 return GL_FALSE;
82
83 pFB = vmesa->driScreen->pFB;
84
85 vmesa->front.offset = fb.offset;
86 vmesa->front.map = (char *)(fb.offset + (GLuint)pFB);
87 vmesa->front.index = fb.index;
88 #ifdef DEBUG
89 if (VIA_DEBUG) {
90 fprintf(stderr, "front offset = %08x\n", vmesa->front.offset);
91 fprintf(stderr, "front index = %d\n", vmesa->front.index);
92 }
93
94
95 if (VIA_DEBUG) fprintf(stderr, "%s - out\n", __FUNCTION__);
96 #endif
97 return GL_TRUE;
98 }
99
100 void
101 via_free_back_buffer(viaContextPtr vmesa)
102 {
103 drm_via_mem_t fb;
104
105 if (!vmesa) return;
106 fb.context = vmesa->hHWContext;
107 fb.index = vmesa->back.index;
108 fb.type = VIDEO;
109 ioctl(vmesa->driFd, DRM_IOCTL_VIA_FREEMEM, &fb);
110 vmesa->back.map = NULL;
111 }
112
113 void
114 via_free_front_buffer(viaContextPtr vmesa)
115 {
116 drm_via_mem_t fb;
117
118 if (!vmesa) return;
119 fb.context = vmesa->hHWContext;
120 fb.index = vmesa->front.index;
121 fb.type = VIDEO;
122 ioctl(vmesa->driFd, DRM_IOCTL_VIA_FREEMEM, &fb);
123 vmesa->front.map = NULL;
124 }
125
126 GLboolean
127 via_alloc_depth_buffer(viaContextPtr vmesa)
128 {
129 drm_via_mem_t fb;
130 unsigned char *pFB;
131 #ifdef DEBUG
132 if (VIA_DEBUG) fprintf(stderr, "%s - in\n", __FUNCTION__);
133 #endif
134 fb.context = vmesa->hHWContext;
135 fb.size = vmesa->depth.size;
136 fb.type = VIDEO;
137
138 if (ioctl(vmesa->driFd, DRM_IOCTL_VIA_ALLOCMEM, &fb)) {
139 return GL_FALSE;
140 }
141
142 pFB = vmesa->driScreen->pFB;
143
144 vmesa->depth.offset = fb.offset;
145 vmesa->depth.map = (char *)(fb.offset + (GLuint)pFB);
146 vmesa->depth.index = fb.index;
147 #ifdef DEBUG
148 if (VIA_DEBUG) {
149 fprintf(stderr, "depth offset = %08x\n", vmesa->depth.offset);
150 fprintf(stderr, "depth index = %d\n", vmesa->depth.index);
151 }
152
153 if (VIA_DEBUG) fprintf(stderr, "%s - out\n", __FUNCTION__);
154 #endif
155 return GL_TRUE;
156 }
157
158 void
159 via_free_depth_buffer(viaContextPtr vmesa)
160 {
161 drm_via_mem_t fb;
162
163 if (!vmesa) return;
164 fb.context = vmesa->hHWContext;
165 fb.index = vmesa->depth.index;
166 fb.type = VIDEO;
167 ioctl(vmesa->driFd, DRM_IOCTL_VIA_FREEMEM, &fb);
168 vmesa->depth.map = NULL;
169 }
170
171 GLboolean
172 via_alloc_dma_buffer(viaContextPtr vmesa)
173 {
174 drmVIADMAInit init;
175
176 #ifdef DEBUG
177 if (VIA_DEBUG) fprintf(stderr, "%s - in\n", __FUNCTION__);
178 #endif
179 vmesa->dma = (GLuint *) malloc(VIA_DMA_BUFSIZ);
180
181 /*
182 * Check whether AGP DMA has been initialized.
183 */
184
185 init.func = VIA_DMA_INITIALIZED;
186 vmesa->useAgp =
187 ( 0 == drmCommandWrite(vmesa->driFd, DRM_VIA_DMA_INIT,
188 &init, sizeof(init)));
189 if (vmesa->useAgp)
190 printf("unichrome_dri.so: Using AGP.\n");
191 else
192 printf("unichrome_dri.so: Using PCI.\n");
193
194 #ifdef DEBUG
195 if (VIA_DEBUG) fprintf(stderr, "%s - out\n", __FUNCTION__);
196 #endif
197 return ((vmesa->dma) ? GL_TRUE : GL_FALSE);
198 }
199
200 void
201 via_free_dma_buffer(viaContextPtr vmesa)
202 {
203 if (!vmesa) return;
204 free(vmesa->dma);
205 vmesa->dma = 0;
206 }
207
208 GLboolean
209 via_alloc_texture(viaContextPtr vmesa, viaTextureObjectPtr t)
210 {
211 drm_via_mem_t fb;
212 #ifdef DEBUG
213 if (VIA_DEBUG) fprintf(stderr, "%s - in\n", __FUNCTION__);
214 #endif
215 fb.context = vmesa->hHWContext;
216 fb.size = t->texMem.size;
217 fb.type = VIDEO;
218 #ifdef DEBUG
219 if (VIA_DEBUG) {
220 fprintf(stderr, "texture size = %d\n", fb.size);
221 fprintf(stderr, "texture type = %d\n", fb.type);
222 }
223 #endif
224 if (ioctl(vmesa->driFd, DRM_IOCTL_VIA_ALLOCMEM, &fb)) {
225 fprintf(stderr, "via_alloc_texture fail\n");
226 return GL_FALSE;
227 }
228
229 t->texMem.offset = fb.offset;
230 t->texMem.index = fb.index;
231 #ifdef DEBUG
232 if (VIA_DEBUG) fprintf(stderr, "texture index = %d\n", (GLuint)fb.index);
233 #endif
234
235 t->bufAddr = (unsigned char *)(fb.offset + (GLuint)vmesa->driScreen->pFB);
236 #ifdef DEBUG
237 if (VIA_DEBUG) fprintf(stderr, "%s - out\n", __FUNCTION__);
238 #endif
239 return GL_TRUE;
240 }
241 /*=* John Sheng [2003.5.31] agp tex *=*/
242 GLboolean
243 via_alloc_texture_agp(viaContextPtr vmesa, viaTextureObjectPtr t)
244 {
245 drm_via_mem_t fb;
246 #ifdef DEBUG
247 if (VIA_DEBUG) fprintf(stderr, "%s - in\n", __FUNCTION__);
248 #endif
249 fb.context = vmesa->hHWContext;
250 fb.size = t->texMem.size;
251 fb.type = AGP;
252 #ifdef DEBUG
253 if (VIA_DEBUG) {
254 fprintf(stderr, "texture_agp size = %d\n", fb.size);
255 fprintf(stderr, "texture type = %d\n", fb.type);
256 }
257 #endif
258 if (ioctl(vmesa->driFd, DRM_IOCTL_VIA_ALLOCMEM, &fb)) {
259 fprintf(stderr, "via_alloc_texture_agp fail\n");
260 return GL_FALSE;
261 }
262
263 t->texMem.offset = fb.offset;
264 t->texMem.index = fb.index;
265 #ifdef DEBUG
266 if (VIA_DEBUG) fprintf(stderr, "texture agp index = %d\n", (GLuint)fb.index);
267 #endif
268
269 t->bufAddr = (unsigned char *)((GLuint)vmesa->viaScreen->agpLinearStart + fb.offset);
270 /*=* John Sheng [2003.5.31] agp tex *=*/
271 t->inAGP = GL_TRUE;
272 #ifdef DEBUG
273 if (VIA_DEBUG) fprintf(stderr, "%s - out\n", __FUNCTION__);
274 #endif
275 return GL_TRUE;
276 }
277
278 void
279 via_free_texture(viaContextPtr vmesa, viaTextureObjectPtr t)
280 {
281 drm_via_mem_t fb;
282 #ifdef DEBUG
283 if (VIA_DEBUG) {
284 fprintf(stderr, "via_free_texture: index = %d\n",
285 t->texMem.index);
286 fprintf(stderr, "via_free_texture: size = %d\n",
287 t->texMem.size);
288 }
289 #endif
290 if (!vmesa) {
291 fprintf(stderr, "!mesa\n");
292 return;
293 }
294
295 fb.context = vmesa->hHWContext;
296 fb.index = t->texMem.index;
297
298 /*=* John Sheng [2003.5.31] agp tex *=*/
299 if(t->inAGP)
300 fb.type = AGP;
301 else
302 fb.type = VIDEO;
303
304 if (ioctl(vmesa->driFd, DRM_IOCTL_VIA_FREEMEM, &fb)) {
305 if(vmesa->shareCtx) {
306 fb.context = ((viaContextPtr)((GLcontext *)(vmesa->shareCtx)->DriverCtx))->hHWContext;
307 if (ioctl(vmesa->driFd, DRM_IOCTL_VIA_FREEMEM, &fb)) {
308 fprintf(stderr, "via_free_texture fail\n");
309 }
310 }
311 else
312 fprintf(stderr, "via_free_texture fail\n");
313 }
314
315 t->bufAddr = NULL;
316 }