vserver 1.9.5.x5
[linux-2.6.git] / drivers / char / drm / sis_mm.c
1 /* sis_mm.c -- Private header for Direct Rendering Manager -*- linux-c -*-
2  * Created: Mon Jan  4 10:05:05 1999 by sclin@sis.com.tw
3  *
4  * Copyright 2000 Silicon Integrated Systems Corp, Inc., HsinChu, Taiwan.
5  * All rights reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation
10  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11  * and/or sell copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following conditions:
13  * 
14  * The above copyright notice and this permission notice (including the next
15  * paragraph) shall be included in all copies or substantial portions of the
16  * Software.
17  * 
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
21  * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  * 
26  * Authors:
27  *    Sung-Ching Lin <sclin@sis.com.tw>
28  * 
29  */
30
31 #include "drmP.h"
32 #include "sis_drm.h"
33 #include "sis_drv.h"
34 #include "sis_ds.h"
35 #if defined(__linux__) && defined(CONFIG_FB_SIS)
36 #include <video/sisfb.h>
37 #endif
38
39 drm_ioctl_desc_t sis_ioctls[] = {
40         [DRM_IOCTL_NR(DRM_SIS_FB_ALLOC)]  = { sis_fb_alloc,        1, 0 },
41         [DRM_IOCTL_NR(DRM_SIS_FB_FREE)]   = { sis_fb_free,         1, 0 },
42         [DRM_IOCTL_NR(DRM_SIS_AGP_INIT)]  = { sis_ioctl_agp_init,  1, 1 },
43         [DRM_IOCTL_NR(DRM_SIS_AGP_ALLOC)] = { sis_ioctl_agp_alloc, 1, 0 },
44         [DRM_IOCTL_NR(DRM_SIS_AGP_FREE)]  = { sis_ioctl_agp_free,  1, 0 },
45         [DRM_IOCTL_NR(DRM_SIS_FB_INIT)]   = { sis_fb_init,         1, 1 }
46 };
47
48 int sis_max_ioctl = DRM_ARRAY_SIZE(sis_ioctls);
49
50 #define MAX_CONTEXT 100
51 #define VIDEO_TYPE 0 
52 #define AGP_TYPE 1
53
54 typedef struct {
55         int used;
56         int context;
57         set_t *sets[2]; /* 0 for video, 1 for AGP */
58 } sis_context_t;
59
60 static sis_context_t global_ppriv[MAX_CONTEXT];
61
62
63 static int add_alloc_set(int context, int type, unsigned int val)
64 {
65         int i, retval = 0;
66         
67         for (i = 0; i < MAX_CONTEXT; i++) {
68                 if (global_ppriv[i].used && global_ppriv[i].context == context)
69                 {
70                         retval = setAdd(global_ppriv[i].sets[type], val);
71                         break;
72                 }
73         }
74         return retval;
75 }
76
77 static int del_alloc_set(int context, int type, unsigned int val)
78 {  
79         int i, retval = 0;
80
81         for (i = 0; i < MAX_CONTEXT; i++) {
82                 if (global_ppriv[i].used && global_ppriv[i].context == context)
83                 {
84                         retval = setDel(global_ppriv[i].sets[type], val);
85                         break;
86                 }
87         }
88         return retval;
89 }
90
91 /* fb management via fb device */ 
92 #if defined(__linux__) && defined(CONFIG_FB_SIS)
93
94 int sis_fb_init( DRM_IOCTL_ARGS )
95 {
96         return 0;
97 }
98
99 int sis_fb_alloc( DRM_IOCTL_ARGS )
100 {
101         drm_sis_mem_t fb;
102         struct sis_memreq req;
103         drm_sis_mem_t __user *argp = (void __user *)data;
104         int retval = 0;
105
106         DRM_COPY_FROM_USER_IOCTL(fb, argp, sizeof(fb));
107
108         req.size = fb.size;
109         sis_malloc(&req);
110         if (req.offset) {
111                 /* TODO */
112                 fb.offset = req.offset;
113                 fb.free = req.offset;
114                 if (!add_alloc_set(fb.context, VIDEO_TYPE, fb.free)) {
115                         DRM_DEBUG("adding to allocation set fails\n");
116                         sis_free(req.offset);
117                         retval = DRM_ERR(EINVAL);
118                 }
119         } else {  
120                 fb.offset = 0;
121                 fb.size = 0;
122                 fb.free = 0;
123         }
124
125         DRM_COPY_TO_USER_IOCTL(argp, fb, sizeof(fb));
126
127         DRM_DEBUG("alloc fb, size = %d, offset = %d\n", fb.size, req.offset);
128
129         return retval;
130 }
131
132 int sis_fb_free( DRM_IOCTL_ARGS )
133 {
134         drm_sis_mem_t fb;
135         int retval = 0;
136
137         DRM_COPY_FROM_USER_IOCTL(fb, (drm_sis_mem_t __user *)data, sizeof(fb));
138
139         if (!fb.free)
140                 return DRM_ERR(EINVAL);
141
142         if (!del_alloc_set(fb.context, VIDEO_TYPE, fb.free))
143                 retval = DRM_ERR(EINVAL);
144         sis_free((u32)fb.free);
145
146         DRM_DEBUG("free fb, offset = %lu\n", fb.free);
147
148         return retval;
149 }
150
151 #else
152
153 /* Called by the X Server to initialize the FB heap.  Allocations will fail
154  * unless this is called.  Offset is the beginning of the heap from the
155  * framebuffer offset (MaxXFBMem in XFree86).
156  *
157  * Memory layout according to Thomas Winischofer:
158  * |------------------|DDDDDDDDDDDDDDDDDDDDDDDDDDDDD|HHHH|CCCCCCCCCCC|
159  *
160  *    X driver/sisfb                                  HW-   Command-
161  *  framebuffer memory           DRI heap           Cursor   queue
162  */
163 int sis_fb_init( DRM_IOCTL_ARGS )
164 {
165         DRM_DEVICE;
166         drm_sis_private_t *dev_priv = dev->dev_private;
167         drm_sis_fb_t fb;
168
169         DRM_COPY_FROM_USER_IOCTL(fb, (drm_sis_fb_t __user *)data, sizeof(fb));
170
171         if (dev_priv == NULL) {
172                 dev->dev_private = drm_calloc(1, sizeof(drm_sis_private_t),
173                     DRM_MEM_DRIVER);
174                 dev_priv = dev->dev_private;
175                 if (dev_priv == NULL)
176                         return ENOMEM;
177         }
178
179         if (dev_priv->FBHeap != NULL)
180                 return DRM_ERR(EINVAL);
181
182         dev_priv->FBHeap = mmInit(fb.offset, fb.size);
183
184         DRM_DEBUG("offset = %u, size = %u", fb.offset, fb.size);
185
186         return 0;
187 }
188
189 int sis_fb_alloc( DRM_IOCTL_ARGS )
190 {
191         DRM_DEVICE;
192         drm_sis_private_t *dev_priv = dev->dev_private;
193         drm_sis_mem_t __user *argp = (void __user *)data;
194         drm_sis_mem_t fb;
195         PMemBlock block;
196         int retval = 0;
197
198         if (dev_priv == NULL || dev_priv->FBHeap == NULL)
199                 return DRM_ERR(EINVAL);
200   
201         DRM_COPY_FROM_USER_IOCTL(fb, argp, sizeof(fb));
202   
203         block = mmAllocMem(dev_priv->FBHeap, fb.size, 0, 0);
204         if (block) {
205                 /* TODO */
206                 fb.offset = block->ofs;
207                 fb.free = (unsigned long)block;
208                 if (!add_alloc_set(fb.context, VIDEO_TYPE, fb.free)) {
209                         DRM_DEBUG("adding to allocation set fails\n");
210                         mmFreeMem((PMemBlock)fb.free);
211                         retval = DRM_ERR(EINVAL);
212                 }
213         } else {
214                 fb.offset = 0;
215                 fb.size = 0;
216                 fb.free = 0;
217         }
218
219         DRM_COPY_TO_USER_IOCTL(argp, fb, sizeof(fb));
220
221         DRM_DEBUG("alloc fb, size = %d, offset = %d\n", fb.size, fb.offset);
222
223         return retval;
224 }
225
226 int sis_fb_free( DRM_IOCTL_ARGS )
227 {
228         DRM_DEVICE;
229         drm_sis_private_t *dev_priv = dev->dev_private;
230         drm_sis_mem_t fb;
231
232         if (dev_priv == NULL || dev_priv->FBHeap == NULL)
233                 return DRM_ERR(EINVAL);
234
235         DRM_COPY_FROM_USER_IOCTL(fb, (drm_sis_mem_t __user *)data, sizeof(fb));
236
237         if (!mmBlockInHeap(dev_priv->FBHeap, (PMemBlock)fb.free))
238                 return DRM_ERR(EINVAL);
239
240         if (!del_alloc_set(fb.context, VIDEO_TYPE, fb.free))
241                 return DRM_ERR(EINVAL);
242         mmFreeMem((PMemBlock)fb.free);
243
244         DRM_DEBUG("free fb, free = 0x%lx\n", fb.free);
245
246         return 0;
247 }
248
249 #endif
250
251 /* agp memory management */ 
252
253 int sis_ioctl_agp_init( DRM_IOCTL_ARGS )
254 {
255         DRM_DEVICE;
256         drm_sis_private_t *dev_priv = dev->dev_private;
257         drm_sis_agp_t agp;
258
259         if (dev_priv == NULL) {
260                 dev->dev_private = drm_calloc(1, sizeof(drm_sis_private_t),
261                     DRM_MEM_DRIVER);
262                 dev_priv = dev->dev_private;
263                 if (dev_priv == NULL)
264                         return ENOMEM;
265         }
266
267         if (dev_priv->AGPHeap != NULL)
268                 return DRM_ERR(EINVAL);
269
270         DRM_COPY_FROM_USER_IOCTL(agp, (drm_sis_agp_t __user *)data, sizeof(agp));
271
272         dev_priv->AGPHeap = mmInit(agp.offset, agp.size);
273
274         DRM_DEBUG("offset = %u, size = %u", agp.offset, agp.size);
275   
276         return 0;
277 }
278
279 int sis_ioctl_agp_alloc( DRM_IOCTL_ARGS )
280 {
281         DRM_DEVICE;
282         drm_sis_private_t *dev_priv = dev->dev_private;
283         drm_sis_mem_t __user *argp = (void __user *)data;
284         drm_sis_mem_t agp;
285         PMemBlock block;
286         int retval = 0;
287    
288         if (dev_priv == NULL || dev_priv->AGPHeap == NULL)
289                 return DRM_ERR(EINVAL);
290   
291         DRM_COPY_FROM_USER_IOCTL(agp, argp, sizeof(agp));
292   
293         block = mmAllocMem(dev_priv->AGPHeap, agp.size, 0, 0);
294         if (block) {
295                 /* TODO */
296                 agp.offset = block->ofs;
297                 agp.free = (unsigned long)block;
298                 if (!add_alloc_set(agp.context, AGP_TYPE, agp.free)) {
299                         DRM_DEBUG("adding to allocation set fails\n");
300                         mmFreeMem((PMemBlock)agp.free);
301                         retval = -1;
302                 }
303         } else {  
304                 agp.offset = 0;
305                 agp.size = 0;
306                 agp.free = 0;
307         }
308
309         DRM_COPY_TO_USER_IOCTL(argp, agp, sizeof(agp));
310
311         DRM_DEBUG("alloc agp, size = %d, offset = %d\n", agp.size, agp.offset);
312
313         return retval;
314 }
315
316 int sis_ioctl_agp_free( DRM_IOCTL_ARGS )
317 {
318         DRM_DEVICE;
319         drm_sis_private_t *dev_priv = dev->dev_private;
320         drm_sis_mem_t agp;
321
322         if (dev_priv == NULL || dev_priv->AGPHeap == NULL)
323                 return DRM_ERR(EINVAL);
324
325         DRM_COPY_FROM_USER_IOCTL(agp, (drm_sis_mem_t __user *)data, sizeof(agp));
326
327         if (!mmBlockInHeap(dev_priv->AGPHeap, (PMemBlock)agp.free))
328                 return DRM_ERR(EINVAL);
329
330         mmFreeMem((PMemBlock)agp.free);
331         if (!del_alloc_set(agp.context, AGP_TYPE, agp.free))
332                 return DRM_ERR(EINVAL);
333
334         DRM_DEBUG("free agp, free = 0x%lx\n", agp.free);
335
336         return 0;
337 }
338
339 int sis_init_context(struct drm_device *dev, int context)
340 {
341         int i;
342
343         for (i = 0; i < MAX_CONTEXT ; i++) {
344                 if (global_ppriv[i].used &&
345                     (global_ppriv[i].context == context))
346                         break;
347         }
348
349         if (i >= MAX_CONTEXT) {
350                 for (i = 0; i < MAX_CONTEXT ; i++) {
351                         if (!global_ppriv[i].used) {
352                                 global_ppriv[i].context = context;
353                                 global_ppriv[i].used = 1;
354                                 global_ppriv[i].sets[0] = setInit();
355                                 global_ppriv[i].sets[1] = setInit();
356                                 DRM_DEBUG("init allocation set, socket=%d, "
357                                     "context = %d\n", i, context);
358                                 break;
359                         }
360                 }
361                 if ((i >= MAX_CONTEXT) || (global_ppriv[i].sets[0] == NULL) ||
362                     (global_ppriv[i].sets[1] == NULL))
363                 {
364                         return 0;
365                 }
366         }
367         
368         return 1;
369 }
370
371 int sis_final_context(struct drm_device *dev, int context)
372 {
373         int i;
374
375         for (i=0; i<MAX_CONTEXT; i++) {
376                 if (global_ppriv[i].used &&
377                     (global_ppriv[i].context == context))
378                         break;
379         }
380
381         if (i < MAX_CONTEXT) {
382                 set_t *set;
383                 unsigned int item;
384                 int retval;
385
386                 DRM_DEBUG("find socket %d, context = %d\n", i, context);
387
388                 /* Video Memory */
389                 set = global_ppriv[i].sets[0];
390                 retval = setFirst(set, &item);
391                 while (retval) {
392                         DRM_DEBUG("free video memory 0x%x\n", item);
393 #if defined(__linux__) && defined(CONFIG_FB_SIS)
394                         sis_free(item);
395 #else
396                         mmFreeMem((PMemBlock)item);
397 #endif
398                         retval = setNext(set, &item);
399                 }
400                 setDestroy(set);
401
402                 /* AGP Memory */
403                 set = global_ppriv[i].sets[1];
404                 retval = setFirst(set, &item);
405                 while (retval) {
406                         DRM_DEBUG("free agp memory 0x%x\n", item);
407                         mmFreeMem((PMemBlock)item);
408                         retval = setNext(set, &item);
409                 }
410                 setDestroy(set);
411
412                 global_ppriv[i].used = 0;         
413         }
414         
415         return 1;
416 }