2 * Copyright 1998-2003 VIA Technologies, Inc. All Rights Reserved.
3 * Copyright 2001-2003 S3 Graphics, Inc. All Rights Reserved.
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:
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
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.
27 #include "via_context.h"
28 #include "via_ioctl.h"
31 #include "main/imports.h"
32 #include "main/simple_list.h"
33 #include <sys/ioctl.h>
36 via_alloc_draw_buffer(struct via_context *vmesa, struct via_renderbuffer *buf)
39 mem.context = vmesa->hHWContext;
41 mem.type = VIA_MEM_VIDEO;
45 if (ioctl(vmesa->driFd, DRM_IOCTL_VIA_ALLOCMEM, &mem))
49 buf->offset = mem.offset;
50 buf->map = (char *)vmesa->driScreen->pFB + mem.offset;
51 buf->index = mem.index;
56 via_free_draw_buffer(struct via_context *vmesa, struct via_renderbuffer *buf)
62 mem.context = vmesa->hHWContext;
63 mem.index = buf->index;
64 mem.type = VIA_MEM_VIDEO;
65 mem.offset = buf->offset;
68 ioctl(vmesa->driFd, DRM_IOCTL_VIA_FREEMEM, &mem);
74 via_alloc_dma_buffer(struct via_context *vmesa)
76 drm_via_dma_init_t init;
78 vmesa->dma = (GLubyte *) malloc(VIA_DMA_BUFSIZ);
81 * Check whether AGP DMA has been initialized.
83 memset(&init, 0, sizeof(init));
84 init.func = VIA_DMA_INITIALIZED;
87 ( 0 == drmCommandWrite(vmesa->driFd, DRM_VIA_DMA_INIT,
88 &init, sizeof(init)));
89 if (VIA_DEBUG & DEBUG_DMA) {
91 fprintf(stderr, "unichrome_dri.so: Using AGP.\n");
93 fprintf(stderr, "unichrome_dri.so: Using PCI.\n");
96 return ((vmesa->dma) ? GL_TRUE : GL_FALSE);
100 via_free_dma_buffer(struct via_context *vmesa)
108 /* These functions now allocate and free the via_tex_buffer struct as well:
110 struct via_tex_buffer *
111 via_alloc_texture(struct via_context *vmesa,
115 struct via_tex_buffer *t = CALLOC_STRUCT(via_tex_buffer);
121 t->memType = memType;
122 insert_at_tail(&vmesa->tex_image_list[memType], t);
124 if (t->memType == VIA_MEM_AGP ||
125 t->memType == VIA_MEM_VIDEO) {
128 fb.context = vmesa->hHWContext;
130 fb.type = t->memType;
134 if (ioctl(vmesa->driFd, DRM_IOCTL_VIA_ALLOCMEM, &fb) != 0 ||
139 fprintf(stderr, "offset %lx index %lx\n", fb.offset, fb.index);
141 t->offset = fb.offset;
144 if (t->memType == VIA_MEM_AGP) {
145 t->bufAddr = (GLubyte *)((unsigned long)vmesa->viaScreen->agpLinearStart +
147 t->texBase = vmesa->agpBase + fb.offset;
150 t->bufAddr = (GLubyte *)((unsigned long)vmesa->driScreen->pFB + fb.offset);
151 t->texBase = fb.offset;
154 vmesa->total_alloc[t->memType] += t->size;
157 else if (t->memType == VIA_MEM_SYSTEM) {
159 t->bufAddr = malloc(t->size);
163 vmesa->total_alloc[t->memType] += t->size;
178 via_do_free_texture(struct via_context *vmesa, struct via_tex_buffer *t)
182 remove_from_list( t );
184 vmesa->total_alloc[t->memType] -= t->size;
186 fb.context = vmesa->hHWContext;
188 fb.offset = t->offset;
189 fb.type = t->memType;
192 if (ioctl(vmesa->driFd, DRM_IOCTL_VIA_FREEMEM, &fb)) {
193 fprintf(stderr, "via_free_texture fail\n");
200 /* Release textures which were potentially still being referenced by
201 * hardware at the time when they were originally freed.
204 via_release_pending_textures( struct via_context *vmesa )
206 struct via_tex_buffer *s, *tmp;
208 foreach_s( s, tmp, &vmesa->freed_tex_buffers ) {
209 if (!VIA_GEQ_WRAP(s->lastUsed, vmesa->lastBreadcrumbRead)) {
210 if (VIA_DEBUG & DEBUG_TEXTURE)
211 fprintf(stderr, "%s: release tex sz %d lastUsed %x\n",
212 __FUNCTION__, s->size, s->lastUsed);
213 via_do_free_texture(vmesa, s);
221 via_free_texture(struct via_context *vmesa, struct via_tex_buffer *t)
226 else if (t->memType == VIA_MEM_SYSTEM) {
228 vmesa->total_alloc[t->memType] -= t->size;
232 else if (t->index && viaCheckBreadcrumb(vmesa, t->lastUsed)) {
233 via_do_free_texture( vmesa, t );
236 /* Close current breadcrumb so that we can free this eventually:
238 if (t->lastUsed == vmesa->lastBreadcrumbWrite)
239 viaEmitBreadcrumb(vmesa);
241 move_to_tail( &vmesa->freed_tex_buffers, t );