OSDN Git Service

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