OSDN Git Service

First check in for DRM that splits core from personality modules
[android-x86/external-libdrm.git] / shared-core / via_mm.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 #include "drmP.h"
25 #include "via_drm.h"
26 #include "via_drv.h"
27 #include "via_ds.h"
28 #include "via_mm.h"
29
30 #define MAX_CONTEXT 100
31
32 unsigned int VIA_DEBUG = 1;
33
34 typedef struct {
35         int used;
36         int context;
37         set_t *sets[2]; /* 0 for frame buffer, 1 for AGP , 2 for System*/
38 } via_context_t;
39
40 static via_context_t global_ppriv[MAX_CONTEXT];
41
42 static int add_alloc_set(int context, int type, unsigned int val)
43 {
44         int i, retval = 0;
45   
46         for (i = 0; i < MAX_CONTEXT; i++) {
47                 if (global_ppriv[i].used && 
48                     global_ppriv[i].context == context) {
49                         retval = via_setAdd(global_ppriv[i].sets[type], val);
50                         break;
51                 }
52         }
53   
54         return retval;
55 }
56
57 static int del_alloc_set(int context, int type, unsigned int val)
58 {  
59         int i, retval = 0;
60   
61         for (i = 0; i < MAX_CONTEXT; i++)
62                 if (global_ppriv[i].used && 
63                     global_ppriv[i].context == context) {
64                         retval = via_setDel(global_ppriv[i].sets[type], val);
65                         break;
66                 }
67   
68         return retval;
69 }
70
71 /* agp memory management */ 
72 static memHeap_t *AgpHeap = NULL;
73
74 int via_agp_init( DRM_IOCTL_ARGS )
75 {
76         drm_via_agp_t agp;
77   
78         DRM_COPY_FROM_USER_IOCTL(agp, (drm_via_agp_t *)data, sizeof(agp));
79
80         AgpHeap = via_mmInit(agp.offset, agp.size);
81
82         DRM_DEBUG("offset = %u, size = %u", agp.offset, agp.size);
83   
84         return 0;
85 }
86
87 /* fb memory management */ 
88 static memHeap_t *FBHeap = NULL;
89
90 int via_fb_init( DRM_IOCTL_ARGS )
91 {
92         drm_via_fb_t fb;
93
94    
95         DRM_COPY_FROM_USER_IOCTL(fb, (drm_via_fb_t *)data, sizeof(fb));
96
97         FBHeap = via_mmInit(fb.offset, fb.size);
98
99         DRM_DEBUG("offset = %u, size = %u", fb.offset, fb.size);
100
101         return 0;
102 }
103
104 int via_init_context(struct drm_device *dev, int context)
105 {
106         int i;
107     
108         for (i = 0; i < MAX_CONTEXT ; i++)
109                 if (global_ppriv[i].used && 
110                     (global_ppriv[i].context == context))
111                         break;
112     
113         if (i >= MAX_CONTEXT) {
114                 for (i = 0; i < MAX_CONTEXT ; i++) {
115                         if (!global_ppriv[i].used) {
116                                 global_ppriv[i].context = context;
117                                 global_ppriv[i].used = 1;
118                                 global_ppriv[i].sets[0] = via_setInit();
119                                 global_ppriv[i].sets[1] = via_setInit();
120                                 DRM_DEBUG("init allocation set, socket=%d,"
121                                           " context = %d\n", i, context);
122                                 break;
123                         }
124                 }
125         
126                 if ((i >= MAX_CONTEXT) || (global_ppriv[i].sets[0] == NULL) ||
127                     (global_ppriv[i].sets[1] == NULL)) {
128                         return 0;
129                 }
130         }
131     
132         return 1;
133 }
134
135 int via_final_context(struct drm_device *dev, int context)
136 {
137         int i;
138         for (i=0; i<MAX_CONTEXT; i++)
139                 if (global_ppriv[i].used && 
140                     (global_ppriv[i].context == context))
141                         break;
142     
143         if (i < MAX_CONTEXT) {
144                 set_t *set;
145                 unsigned int item;
146                 int retval;
147           
148                 DRM_DEBUG("find socket %d, context = %d\n", i, context);
149             
150                 /* Video Memory */
151                 set = global_ppriv[i].sets[0];
152                 retval = via_setFirst(set, &item);
153                 while (retval) {
154                         DRM_DEBUG("free video memory 0x%x\n", item);
155                         via_mmFreeMem((PMemBlock)item);
156                         retval = via_setNext(set, &item);
157                 }
158                 via_setDestroy(set);
159             
160                 /* AGP Memory */
161                 set = global_ppriv[i].sets[1];
162                 retval = via_setFirst(set, &item);
163                 while (retval) {
164                         DRM_DEBUG("free agp memory 0x%x\n", item);
165                         via_mmFreeMem((PMemBlock)item);
166                         retval = via_setNext(set, &item);
167                 }
168                 via_setDestroy(set);
169         
170                 global_ppriv[i].used = 0;         
171         }
172
173 #if defined(__linux__)
174         /* Linux specific until context tracking code gets ported to BSD */
175         /* Last context, perform cleanup */
176         if (dev->ctx_count == 1 && dev->dev_private) {
177                 if (dev->irq) drm_irq_uninstall(dev);
178
179                 via_do_cleanup_map(dev);
180         }
181 #endif
182     
183         return 1;
184 }
185 int via_mem_alloc( DRM_IOCTL_ARGS)
186 {
187         drm_via_mem_t mem;
188     
189         DRM_COPY_FROM_USER_IOCTL(mem, (drm_via_mem_t *)data, sizeof(mem));
190         switch (mem.type) {
191         case VIDEO :
192                 if (via_fb_alloc(&mem) < 0)
193                         return -EFAULT;
194                 DRM_COPY_TO_USER_IOCTL((drm_via_mem_t *)data, mem, 
195                                        sizeof(mem));
196                 return 0;
197         case AGP :
198                 if (via_agp_alloc(&mem) < 0)
199                         return -EFAULT;
200                 DRM_COPY_TO_USER_IOCTL((drm_via_mem_t *)data, mem, 
201                                        sizeof(mem));
202                 return 0;
203         }
204
205         return -EFAULT;
206 }
207
208 int via_fb_alloc(drm_via_mem_t* mem)
209 {
210         drm_via_mm_t fb;
211         PMemBlock block;
212         int retval = 0;
213
214         if (!FBHeap)
215                 return -1;
216
217         fb.size = mem->size;
218         fb.context = mem->context;
219
220         block = via_mmAllocMem(FBHeap, fb.size, 5, 0);
221         if (block) {
222                 fb.offset = block->ofs;
223                 fb.free = (unsigned int)block;
224                 if (!add_alloc_set(fb.context, VIDEO, fb.free)) {
225                         DRM_DEBUG("adding to allocation set fails\n");
226                         via_mmFreeMem((PMemBlock)fb.free);
227                         retval = -1;
228                 }
229         }
230         else {  
231                 fb.offset = 0;
232                 fb.size = 0;
233                 fb.free = 0;
234                 retval = -1;
235         }
236
237         mem->offset = fb.offset;
238         mem->index = fb.free;
239
240         DRM_DEBUG("alloc fb, size = %d, offset = %d\n", fb.size, 
241                   (int)fb.offset);
242
243         return retval;
244 }
245 int via_agp_alloc(drm_via_mem_t* mem)
246 {
247         drm_via_mm_t agp;
248         PMemBlock block;
249         int retval = 0;
250
251         if (!AgpHeap)
252                 return -1;
253
254         agp.size = mem->size;
255         agp.context = mem->context;
256   
257         block = via_mmAllocMem(AgpHeap, agp.size, 5, 0);
258         if (block) {
259                 agp.offset = block->ofs;
260                 agp.free = (unsigned int)block;
261                 if (!add_alloc_set(agp.context, AGP, agp.free)) {
262                         DRM_DEBUG("adding to allocation set fails\n");
263                         via_mmFreeMem((PMemBlock)agp.free);
264                         retval = -1;
265                 }
266         }
267         else {  
268                 agp.offset = 0;
269                 agp.size = 0;
270                 agp.free = 0;
271         }       
272
273         mem->offset = agp.offset;
274         mem->index = agp.free;
275
276         DRM_DEBUG("alloc agp, size = %d, offset = %d\n", agp.size, 
277                   (unsigned int)agp.offset);
278         return retval;
279 }
280
281 int via_mem_free( DRM_IOCTL_ARGS )
282 {
283         drm_via_mem_t mem;
284
285         DRM_COPY_FROM_USER_IOCTL(mem, (drm_via_mem_t *)data, sizeof(mem));
286
287         switch (mem.type) {
288
289         case VIDEO :
290                 if (via_fb_free(&mem) == 0)
291                         return 0;
292                 break;
293         case AGP :
294                 if (via_agp_free(&mem) == 0)
295                         return 0;
296                 break;
297         }
298
299         return -EFAULT;
300 }
301
302 int via_fb_free(drm_via_mem_t* mem)
303 {
304         drm_via_mm_t fb;
305         int retval = 0;
306
307
308         if (!FBHeap) {
309                 return -1;
310         }
311
312         fb.free = mem->index;
313         fb.context = mem->context;
314
315         if (!fb.free)
316                 {
317                         return -1;
318
319                 }
320
321         via_mmFreeMem((PMemBlock)fb.free);
322
323         if (!del_alloc_set(fb.context, VIDEO, fb.free))
324                 {
325                         retval = -1;
326                 }
327
328         DRM_DEBUG("free fb, free = %d\n", fb.free);
329
330         return retval;
331 }
332
333 int via_agp_free(drm_via_mem_t* mem)
334 {
335         drm_via_mm_t agp;
336
337         int retval = 0;
338
339         agp.free = mem->index;
340         agp.context = mem->context;
341
342         if (!agp.free)
343                 return -1;
344
345         via_mmFreeMem((PMemBlock)agp.free);
346
347         if (!del_alloc_set(agp.context, AGP, agp.free)) {
348                 retval = -1;
349         }
350
351         DRM_DEBUG("free agp, free = %d\n", agp.free);
352
353         return retval;
354 }
355