summaryrefslogtreecommitdiff
path: root/src/mesa/drivers/dri/unichrome/via_fb.c
blob: e07c200b3210e5b381e5929c84bf9fe9a764c81e (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
/*
 * Copyright 1998-2003 VIA Technologies, Inc. All Rights Reserved.
 * Copyright 2001-2003 S3 Graphics, Inc. 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
 * 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
 * VIA, S3 GRAPHICS, AND/OR ITS 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.
 */

#include <assert.h>

#include "via_context.h"
#include "via_ioctl.h"
#include "via_fb.h"
#include "xf86drm.h"
#include "imports.h"
#include "simple_list.h"
#include <sys/ioctl.h>

GLboolean
via_alloc_draw_buffer(struct via_context *vmesa, struct via_buffer *buf)
{
   drm_via_mem_t mem;
   mem.context = vmesa->hHWContext;
   mem.size = buf->size;
   mem.type = VIA_MEM_VIDEO;
   mem.offset = 0;
   mem.index = 0;

   if (ioctl(vmesa->driFd, DRM_IOCTL_VIA_ALLOCMEM, &mem)) 
      return GL_FALSE;
    
    
   buf->offset = mem.offset;
   buf->map = (char *)vmesa->driScreen->pFB + mem.offset;
   buf->index = mem.index;
   return GL_TRUE;
}

void
via_free_draw_buffer(struct via_context *vmesa, struct via_buffer *buf)
{
   drm_via_mem_t mem;

   if (!vmesa) return;

   mem.context = vmesa->hHWContext;
   mem.index = buf->index;
   mem.type = VIA_MEM_VIDEO;
   mem.offset = buf->offset;
   mem.size = buf->size;

   ioctl(vmesa->driFd, DRM_IOCTL_VIA_FREEMEM, &mem);
   buf->map = NULL;
}


GLboolean
via_alloc_dma_buffer(struct via_context *vmesa)
{
   drm_via_dma_init_t init;

   vmesa->dma = (GLubyte *) malloc(VIA_DMA_BUFSIZ);
    
   /*
    * Check whether AGP DMA has been initialized.
    */
   memset(&init, 0, sizeof(init));
   init.func = VIA_DMA_INITIALIZED;

   vmesa->useAgp = 
     ( 0 == drmCommandWrite(vmesa->driFd, DRM_VIA_DMA_INIT, 
			     &init, sizeof(init)));
   if (VIA_DEBUG & DEBUG_DMA) {
      if (vmesa->useAgp) 
         fprintf(stderr, "unichrome_dri.so: Using AGP.\n");
      else
         fprintf(stderr, "unichrome_dri.so: Using PCI.\n");
   }
      
   return ((vmesa->dma) ? GL_TRUE : GL_FALSE);
}

void
via_free_dma_buffer(struct via_context *vmesa)
{
    if (!vmesa) return;
    free(vmesa->dma);
    vmesa->dma = 0;
} 


/* These functions now allocate and free the via_tex_buffer struct as well:
 */
struct via_tex_buffer *
via_alloc_texture(struct via_context *vmesa,
		  GLuint size,
		  GLuint memType)
{
   struct via_tex_buffer *t = CALLOC_STRUCT(via_tex_buffer);
   
   if (!t)
      goto cleanup;

   t->size = size;
   t->memType = memType;
   insert_at_tail(&vmesa->tex_image_list[memType], t);

   if (t->memType == VIA_MEM_AGP || 
       t->memType == VIA_MEM_VIDEO) {
      drm_via_mem_t fb;

      fb.context = vmesa->hHWContext;
      fb.size = t->size;
      fb.type = t->memType;
      fb.offset = 0;
      fb.index = 0;

      if (ioctl(vmesa->driFd, DRM_IOCTL_VIA_ALLOCMEM, &fb) != 0 || 
	  fb.index == 0) 
	 goto cleanup;

      if (0)
	 fprintf(stderr, "offset %lx index %lx\n", fb.offset, fb.index);

      t->offset = fb.offset;
      t->index = fb.index;
      
      if (t->memType == VIA_MEM_AGP) {
	 t->bufAddr = (GLubyte *)((GLuint)vmesa->viaScreen->agpLinearStart +
				  fb.offset); 	
	 t->texBase = (GLuint)vmesa->agpBase + fb.offset;
      }
      else {
	 t->bufAddr = (GLubyte *)(fb.offset + (GLuint)vmesa->driScreen->pFB);
	 t->texBase = fb.offset;
      }

      vmesa->total_alloc[t->memType] += t->size;
      return t;
   }
   else if (t->memType == VIA_MEM_SYSTEM) {
      
      t->bufAddr = _mesa_malloc(t->size);      
      if (!t->bufAddr)
	 goto cleanup;

      vmesa->total_alloc[t->memType] += t->size;
      return t;
   }

 cleanup:
   if (t) {
      remove_from_list(t);
      FREE(t);
   }

   return NULL;
}


static void
via_do_free_texture(struct via_context *vmesa, struct via_tex_buffer *t)
{
   drm_via_mem_t fb;

   remove_from_list( t );

   vmesa->total_alloc[t->memType] -= t->size;

   fb.context = vmesa->hHWContext;
   fb.index = t->index;
   fb.offset = t->offset;
   fb.type = t->memType;
   fb.size = t->size;

   if (ioctl(vmesa->driFd, DRM_IOCTL_VIA_FREEMEM, &fb)) {
      fprintf(stderr, "via_free_texture fail\n");
   }

   FREE(t);
}


/* Release textures which were potentially still being referenced by
 * hardware at the time when they were originally freed.
 */
void 
via_release_pending_textures( struct via_context *vmesa )
{
   struct via_tex_buffer *s, *tmp;
   
   foreach_s( s, tmp, &vmesa->freed_tex_buffers ) {
      if (s->lastUsed < vmesa->lastBreadcrumbRead) {
	 if (VIA_DEBUG & DEBUG_TEXTURE)
	    fprintf(stderr, "%s: release tex sz %d lastUsed %x\n",
		    __FUNCTION__, s->size, s->lastUsed); 
	 via_do_free_texture(vmesa, s);
      }
   }
}
      


void
via_free_texture(struct via_context *vmesa, struct via_tex_buffer *t)
{
   if (!t) {
      return;
   }
   else if (t->memType == VIA_MEM_SYSTEM) {
      remove_from_list(t);
      vmesa->total_alloc[t->memType] -= t->size;
      _mesa_free(t->bufAddr);
      _mesa_free(t);
   }
   else if (t->index && viaCheckBreadcrumb(vmesa, t->lastUsed)) {
      via_do_free_texture( vmesa, t );
   }
   else {
      /* Close current breadcrumb so that we can free this eventually:
       */
      if (t->lastUsed == vmesa->lastBreadcrumbWrite) 
	 viaEmitBreadcrumb(vmesa);

      move_to_tail( &vmesa->freed_tex_buffers, t );
   }
}