Large update
[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 if (VIA_DEBUG) fprintf(stderr, "%s - in\n", __FUNCTION__);
39 fb.context = vmesa->hHWContext;
40 fb.size = vmesa->back.size;
41 fb.type = VIDEO;
42 if (VIA_DEBUG) fprintf(stderr, "context = %d, size =%d, type = %d\n", fb.context, fb.size, fb.type);
43 if (ioctl(vmesa->driFd, DRM_IOCTL_VIA_ALLOCMEM, &fb))
44 return GL_FALSE;
45
46 pFB = vmesa->driScreen->pFB;
47
48 vmesa->back.offset = fb.offset;
49 vmesa->back.map = (char *)(fb.offset + (GLuint)pFB);
50 vmesa->back.index = fb.index;
51 if (VIA_DEBUG) {
52 fprintf(stderr, "back offset = %08x\n", vmesa->back.offset);
53 fprintf(stderr, "back index = %d\n", vmesa->back.index);
54 }
55
56 if (VIA_DEBUG) fprintf(stderr, "%s - out\n", __FUNCTION__);
57 return GL_TRUE;
58 }
59
60 GLboolean
61 via_alloc_front_buffer(viaContextPtr vmesa)
62 {
63 drm_via_mem_t fb;
64 unsigned char *pFB;
65 if (VIA_DEBUG) fprintf(stderr, "%s - in\n", __FUNCTION__);
66 fb.context = vmesa->hHWContext;
67 fb.size = vmesa->back.size;
68 fb.type = VIDEO;
69 if (VIA_DEBUG) fprintf(stderr, "context = %d, size =%d, type = %d\n", fb.context, fb.size, fb.type);
70 if (ioctl(vmesa->driFd, DRM_IOCTL_VIA_ALLOCMEM, &fb))
71 return GL_FALSE;
72
73 pFB = vmesa->driScreen->pFB;
74
75 vmesa->front.offset = fb.offset;
76 vmesa->front.map = (char *)(fb.offset + (GLuint)pFB);
77 vmesa->front.index = fb.index;
78 if (VIA_DEBUG) {
79 fprintf(stderr, "front offset = %08x\n", vmesa->front.offset);
80 fprintf(stderr, "front index = %d\n", vmesa->front.index);
81 }
82
83
84 if (VIA_DEBUG) fprintf(stderr, "%s - out\n", __FUNCTION__);
85 return GL_TRUE;
86 }
87
88 void
89 via_free_back_buffer(viaContextPtr vmesa)
90 {
91 drm_via_mem_t fb;
92
93 if (!vmesa) return;
94 fb.context = vmesa->hHWContext;
95 fb.index = vmesa->back.index;
96 fb.type = VIDEO;
97 ioctl(vmesa->driFd, DRM_IOCTL_VIA_FREEMEM, &fb);
98 vmesa->back.map = NULL;
99 }
100
101 void
102 via_free_front_buffer(viaContextPtr vmesa)
103 {
104 drm_via_mem_t fb;
105
106 if (!vmesa) return;
107 fb.context = vmesa->hHWContext;
108 fb.index = vmesa->front.index;
109 fb.type = VIDEO;
110 ioctl(vmesa->driFd, DRM_IOCTL_VIA_FREEMEM, &fb);
111 vmesa->front.map = NULL;
112 }
113
114 GLboolean
115 via_alloc_depth_buffer(viaContextPtr vmesa)
116 {
117 drm_via_mem_t fb;
118 unsigned char *pFB;
119 if (VIA_DEBUG) fprintf(stderr, "%s - in\n", __FUNCTION__);
120 fb.context = vmesa->hHWContext;
121 fb.size = vmesa->depth.size;
122 fb.type = VIDEO;
123
124 if (ioctl(vmesa->driFd, DRM_IOCTL_VIA_ALLOCMEM, &fb)) {
125 return GL_FALSE;
126 }
127
128 pFB = vmesa->driScreen->pFB;
129
130 vmesa->depth.offset = fb.offset;
131 vmesa->depth.map = (char *)(fb.offset + (GLuint)pFB);
132 vmesa->depth.index = fb.index;
133 if (VIA_DEBUG) {
134 fprintf(stderr, "depth offset = %08x\n", vmesa->depth.offset);
135 fprintf(stderr, "depth index = %d\n", vmesa->depth.index);
136 }
137
138 if (VIA_DEBUG) fprintf(stderr, "%s - out\n", __FUNCTION__);
139 return GL_TRUE;
140 }
141
142 void
143 via_free_depth_buffer(viaContextPtr vmesa)
144 {
145 drm_via_mem_t fb;
146
147 if (!vmesa) return;
148 fb.context = vmesa->hHWContext;
149 fb.index = vmesa->depth.index;
150 fb.type = VIDEO;
151 ioctl(vmesa->driFd, DRM_IOCTL_VIA_FREEMEM, &fb);
152 vmesa->depth.map = NULL;
153 }
154
155 GLboolean
156 via_alloc_dma_buffer(viaContextPtr vmesa)
157 {
158 drmVIADMAInit init;
159
160 if (VIA_DEBUG) fprintf(stderr, "%s - in\n", __FUNCTION__);
161 vmesa->dma = (GLuint *) malloc(VIA_DMA_BUFSIZ);
162
163 /*
164 * Check whether AGP DMA has been initialized.
165 */
166 init.func = VIA_DMA_INITIALIZED;
167 vmesa->useAgp =
168 ( 0 == drmCommandWrite(vmesa->driFd, DRM_VIA_DMA_INIT,
169 &init, sizeof(init)));
170 if (vmesa->useAgp)
171 printf("unichrome_dri.so: Using AGP.\n");
172 else
173 printf("unichrome_dri.so: Using PCI.\n");
174
175 if (VIA_DEBUG) fprintf(stderr, "%s - out\n", __FUNCTION__);
176 return ((vmesa->dma) ? GL_TRUE : GL_FALSE);
177 }
178
179 void
180 via_free_dma_buffer(viaContextPtr vmesa)
181 {
182 if (!vmesa) return;
183 free(vmesa->dma);
184 vmesa->dma = 0;
185 }
186
187 GLboolean
188 via_alloc_texture(viaContextPtr vmesa, viaTextureObjectPtr t)
189 {
190 drm_via_mem_t fb;
191 if (VIA_DEBUG) fprintf(stderr, "%s - in\n", __FUNCTION__);
192 fb.context = vmesa->hHWContext;
193 fb.size = t->texMem.size;
194 fb.type = VIDEO;
195 if (VIA_DEBUG) {
196 fprintf(stderr, "texture size = %d\n", fb.size);
197 fprintf(stderr, "texture type = %d\n", fb.type);
198 }
199 if (ioctl(vmesa->driFd, DRM_IOCTL_VIA_ALLOCMEM, &fb)) {
200 fprintf(stderr, "via_alloc_texture fail\n");
201 return GL_FALSE;
202 }
203
204 t->texMem.offset = fb.offset;
205 t->texMem.index = fb.index;
206 if (VIA_DEBUG) fprintf(stderr, "texture index = %d\n", (GLuint)fb.index);
207
208 t->bufAddr = (unsigned char *)(fb.offset + (GLuint)vmesa->driScreen->pFB);
209 if (VIA_DEBUG) fprintf(stderr, "%s - out\n", __FUNCTION__);
210 return GL_TRUE;
211 }
212 /*=* John Sheng [2003.5.31] agp tex *=*/
213 GLboolean
214 via_alloc_texture_agp(viaContextPtr vmesa, viaTextureObjectPtr t)
215 {
216 drm_via_mem_t fb;
217 if (VIA_DEBUG) fprintf(stderr, "%s - in\n", __FUNCTION__);
218 fb.context = vmesa->hHWContext;
219 fb.size = t->texMem.size;
220 fb.type = AGP;
221 if (VIA_DEBUG) {
222 fprintf(stderr, "texture_agp size = %d\n", fb.size);
223 fprintf(stderr, "texture type = %d\n", fb.type);
224 }
225 if (ioctl(vmesa->driFd, DRM_IOCTL_VIA_ALLOCMEM, &fb)) {
226 fprintf(stderr, "via_alloc_texture_agp fail\n");
227 return GL_FALSE;
228 }
229
230 t->texMem.offset = fb.offset;
231 t->texMem.index = fb.index;
232 if (VIA_DEBUG) fprintf(stderr, "texture agp index = %d\n", (GLuint)fb.index);
233
234 t->bufAddr = (unsigned char *)((GLuint)vmesa->viaScreen->agpLinearStart + fb.offset);
235 /*=* John Sheng [2003.5.31] agp tex *=*/
236 t->inAGP = GL_TRUE;
237 if (VIA_DEBUG) fprintf(stderr, "%s - out\n", __FUNCTION__);
238 return GL_TRUE;
239 }
240
241 void
242 via_free_texture(viaContextPtr vmesa, viaTextureObjectPtr t)
243 {
244 drm_via_mem_t fb;
245 if (VIA_DEBUG) {
246 fprintf(stderr, "via_free_texture: index = %d\n",
247 t->texMem.index);
248 fprintf(stderr, "via_free_texture: size = %d\n",
249 t->texMem.size);
250 }
251 if (!vmesa) {
252 fprintf(stderr, "!mesa\n");
253 return;
254 }
255
256 fb.context = vmesa->hHWContext;
257 fb.index = t->texMem.index;
258
259 /*=* John Sheng [2003.5.31] agp tex *=*/
260 if(t->inAGP)
261 fb.type = AGP;
262 else
263 fb.type = VIDEO;
264
265 if (ioctl(vmesa->driFd, DRM_IOCTL_VIA_FREEMEM, &fb)) {
266 if(vmesa->shareCtx) {
267 fb.context = ((viaContextPtr)((GLcontext *)(vmesa->shareCtx)->DriverCtx))->hHWContext;
268 if (ioctl(vmesa->driFd, DRM_IOCTL_VIA_FREEMEM, &fb)) {
269 fprintf(stderr, "via_free_texture fail\n");
270 }
271 }
272 else
273 fprintf(stderr, "via_free_texture fail\n");
274 }
275
276 t->bufAddr = NULL;
277 }