vserver 1.9.3
[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 "sis.h"
32 #include "drmP.h"
33 #include "sis_drm.h"
34 #include "sis_drv.h"
35 #include "sis_ds.h"
36 #if defined(__linux__) && defined(CONFIG_FB_SIS)
37 #include <video/sisfb.h>
38 #endif
39
40 #define MAX_CONTEXT 100
41 #define VIDEO_TYPE 0 
42 #define AGP_TYPE 1
43
44 typedef struct {
45         int used;
46         int context;
47         set_t *sets[2]; /* 0 for video, 1 for AGP */
48 } sis_context_t;
49
50 static sis_context_t global_ppriv[MAX_CONTEXT];
51
52
53 static int add_alloc_set(int context, int type, unsigned int val)
54 {
55         int i, retval = 0;
56         
57         for (i = 0; i < MAX_CONTEXT; i++) {
58                 if (global_ppriv[i].used && global_ppriv[i].context == context)
59                 {
60                         retval = setAdd(global_ppriv[i].sets[type], val);
61                         break;
62                 }
63         }
64         return retval;
65 }
66
67 static int del_alloc_set(int context, int type, unsigned int val)
68 {  
69         int i, retval = 0;
70
71         for (i = 0; i < MAX_CONTEXT; i++) {
72                 if (global_ppriv[i].used && global_ppriv[i].context == context)
73                 {
74                         retval = setDel(global_ppriv[i].sets[type], val);
75                         break;
76                 }
77         }
78         return retval;
79 }
80
81 /* fb management via fb device */ 
82 #if defined(__linux__) && defined(CONFIG_FB_SIS)
83
84 int sis_fb_init( DRM_IOCTL_ARGS )
85 {
86         return 0;
87 }
88
89 int sis_fb_alloc( DRM_IOCTL_ARGS )
90 {
91         drm_sis_mem_t fb;
92         struct sis_memreq req;
93         drm_sis_mem_t __user *argp = (void __user *)data;
94         int retval = 0;
95
96         DRM_COPY_FROM_USER_IOCTL(fb, argp, sizeof(fb));
97
98         req.size = fb.size;
99         sis_malloc(&req);
100         if (req.offset) {
101                 /* TODO */
102                 fb.offset = req.offset;
103                 fb.free = req.offset;
104                 if (!add_alloc_set(fb.context, VIDEO_TYPE, fb.free)) {
105                         DRM_DEBUG("adding to allocation set fails\n");
106                         sis_free(req.offset);
107                         retval = DRM_ERR(EINVAL);
108                 }
109         } else {  
110                 fb.offset = 0;
111                 fb.size = 0;
112                 fb.free = 0;
113         }
114
115         DRM_COPY_TO_USER_IOCTL(argp, fb, sizeof(fb));
116
117         DRM_DEBUG("alloc fb, size = %d, offset = %d\n", fb.size, req.offset);
118
119         return retval;
120 }
121
122 int sis_fb_free( DRM_IOCTL_ARGS )
123 {
124         drm_sis_mem_t fb;
125         int retval = 0;
126
127         DRM_COPY_FROM_USER_IOCTL(fb, (drm_sis_mem_t __user *)data, sizeof(fb));
128
129         if (!fb.free)
130                 return DRM_ERR(EINVAL);
131
132         if (!del_alloc_set(fb.context, VIDEO_TYPE, fb.free))
133                 retval = DRM_ERR(EINVAL);
134         sis_free((u32)fb.free);
135
136         DRM_DEBUG("free fb, offset = %lu\n", fb.free);
137
138         return retval;
139 }
140
141 #else
142
143 /* Called by the X Server to initialize the FB heap.  Allocations will fail
144  * unless this is called.  Offset is the beginning of the heap from the
145  * framebuffer offset (MaxXFBMem in XFree86).
146  *
147  * Memory layout according to Thomas Winischofer:
148  * |------------------|DDDDDDDDDDDDDDDDDDDDDDDDDDDDD|HHHH|CCCCCCCCCCC|
149  *
150  *    X driver/sisfb                                  HW-   Command-
151  *  framebuffer memory           DRI heap           Cursor   queue
152  */
153 int sis_fb_init( DRM_IOCTL_ARGS )
154 {
155         DRM_DEVICE;
156         drm_sis_private_t *dev_priv = dev->dev_private;
157         drm_sis_fb_t fb;
158
159         DRM_COPY_FROM_USER_IOCTL(fb, (drm_sis_fb_t __user *)data, sizeof(fb));
160
161         if (dev_priv == NULL) {
162                 dev->dev_private = DRM(calloc)(1, sizeof(drm_sis_private_t),
163                     DRM_MEM_DRIVER);
164                 dev_priv = dev->dev_private;
165                 if (dev_priv == NULL)
166                         return ENOMEM;
167         }
168
169         if (dev_priv->FBHeap != NULL)
170                 return DRM_ERR(EINVAL);
171
172         dev_priv->FBHeap = mmInit(fb.offset, fb.size);
173
174         DRM_DEBUG("offset = %u, size = %u", fb.offset, fb.size);
175
176         return 0;
177 }
178
179 int sis_fb_alloc( DRM_IOCTL_ARGS )
180 {
181         DRM_DEVICE;
182         drm_sis_private_t *dev_priv = dev->dev_private;
183         drm_sis_mem_t __user *argp = (void __user *)data;
184         drm_sis_mem_t fb;
185         PMemBlock block;
186         int retval = 0;
187
188         if (dev_priv == NULL || dev_priv->FBHeap == NULL)
189                 return DRM_ERR(EINVAL);
190   
191         DRM_COPY_FROM_USER_IOCTL(fb, argp, sizeof(fb));
192   
193         block = mmAllocMem(dev_priv->FBHeap, fb.size, 0, 0);
194         if (block) {
195                 /* TODO */
196                 fb.offset = block->ofs;
197                 fb.free = (unsigned long)block;
198                 if (!add_alloc_set(fb.context, VIDEO_TYPE, fb.free)) {
199                         DRM_DEBUG("adding to allocation set fails\n");
200                         mmFreeMem((PMemBlock)fb.free);
201                         retval = DRM_ERR(EINVAL);
202                 }
203         } else {
204                 fb.offset = 0;
205                 fb.size = 0;
206                 fb.free = 0;
207         }
208
209         DRM_COPY_TO_USER_IOCTL(argp, fb, sizeof(fb));
210
211         DRM_DEBUG("alloc fb, size = %d, offset = %d\n", fb.size, fb.offset);
212
213         return retval;
214 }
215
216 int sis_fb_free( DRM_IOCTL_ARGS )
217 {
218         DRM_DEVICE;
219         drm_sis_private_t *dev_priv = dev->dev_private;
220         drm_sis_mem_t fb;
221
222         if (dev_priv == NULL || dev_priv->FBHeap == NULL)
223                 return DRM_ERR(EINVAL);
224
225         DRM_COPY_FROM_USER_IOCTL(fb, (drm_sis_mem_t __user *)data, sizeof(fb));
226
227         if (!mmBlockInHeap(dev_priv->FBHeap, (PMemBlock)fb.free))
228                 return DRM_ERR(EINVAL);
229
230         if (!del_alloc_set(fb.context, VIDEO_TYPE, fb.free))
231                 return DRM_ERR(EINVAL);
232         mmFreeMem((PMemBlock)fb.free);
233
234         DRM_DEBUG("free fb, free = 0x%lx\n", fb.free);
235
236         return 0;
237 }
238
239 #endif
240
241 /* agp memory management */ 
242
243 int sis_ioctl_agp_init( DRM_IOCTL_ARGS )
244 {
245         DRM_DEVICE;
246         drm_sis_private_t *dev_priv = dev->dev_private;
247         drm_sis_agp_t agp;
248
249         if (dev_priv == NULL) {
250                 dev->dev_private = DRM(calloc)(1, sizeof(drm_sis_private_t),
251                     DRM_MEM_DRIVER);
252                 dev_priv = dev->dev_private;
253                 if (dev_priv == NULL)
254                         return ENOMEM;
255         }
256
257         if (dev_priv->AGPHeap != NULL)
258                 return DRM_ERR(EINVAL);
259
260         DRM_COPY_FROM_USER_IOCTL(agp, (drm_sis_agp_t __user *)data, sizeof(agp));
261
262         dev_priv->AGPHeap = mmInit(agp.offset, agp.size);
263
264         DRM_DEBUG("offset = %u, size = %u", agp.offset, agp.size);
265   
266         return 0;
267 }
268
269 int sis_ioctl_agp_alloc( DRM_IOCTL_ARGS )
270 {
271         DRM_DEVICE;
272         drm_sis_private_t *dev_priv = dev->dev_private;
273         drm_sis_mem_t __user *argp = (void __user *)data;
274         drm_sis_mem_t agp;
275         PMemBlock block;
276         int retval = 0;
277    
278         if (dev_priv == NULL || dev_priv->AGPHeap == NULL)
279                 return DRM_ERR(EINVAL);
280   
281         DRM_COPY_FROM_USER_IOCTL(agp, argp, sizeof(agp));
282   
283         block = mmAllocMem(dev_priv->AGPHeap, agp.size, 0, 0);
284         if (block) {
285                 /* TODO */
286                 agp.offset = block->ofs;
287                 agp.free = (unsigned long)block;
288                 if (!add_alloc_set(agp.context, AGP_TYPE, agp.free)) {
289                         DRM_DEBUG("adding to allocation set fails\n");
290                         mmFreeMem((PMemBlock)agp.free);
291                         retval = -1;
292                 }
293         } else {  
294                 agp.offset = 0;
295                 agp.size = 0;
296                 agp.free = 0;
297         }
298
299         DRM_COPY_TO_USER_IOCTL(argp, agp, sizeof(agp));
300
301         DRM_DEBUG("alloc agp, size = %d, offset = %d\n", agp.size, agp.offset);
302
303         return retval;
304 }
305
306 int sis_ioctl_agp_free( DRM_IOCTL_ARGS )
307 {
308         DRM_DEVICE;
309         drm_sis_private_t *dev_priv = dev->dev_private;
310         drm_sis_mem_t agp;
311
312         if (dev_priv == NULL || dev_priv->AGPHeap == NULL)
313                 return DRM_ERR(EINVAL);
314
315         DRM_COPY_FROM_USER_IOCTL(agp, (drm_sis_mem_t __user *)data, sizeof(agp));
316
317         if (!mmBlockInHeap(dev_priv->AGPHeap, (PMemBlock)agp.free))
318                 return DRM_ERR(EINVAL);
319
320         mmFreeMem((PMemBlock)agp.free);
321         if (!del_alloc_set(agp.context, AGP_TYPE, agp.free))
322                 return DRM_ERR(EINVAL);
323
324         DRM_DEBUG("free agp, free = 0x%lx\n", agp.free);
325
326         return 0;
327 }
328
329 int sis_init_context(struct drm_device *dev, int context)
330 {
331         int i;
332
333         for (i = 0; i < MAX_CONTEXT ; i++) {
334                 if (global_ppriv[i].used &&
335                     (global_ppriv[i].context == context))
336                         break;
337         }
338
339         if (i >= MAX_CONTEXT) {
340                 for (i = 0; i < MAX_CONTEXT ; i++) {
341                         if (!global_ppriv[i].used) {
342                                 global_ppriv[i].context = context;
343                                 global_ppriv[i].used = 1;
344                                 global_ppriv[i].sets[0] = setInit();
345                                 global_ppriv[i].sets[1] = setInit();
346                                 DRM_DEBUG("init allocation set, socket=%d, "
347                                     "context = %d\n", i, context);
348                                 break;
349                         }
350                 }
351                 if ((i >= MAX_CONTEXT) || (global_ppriv[i].sets[0] == NULL) ||
352                     (global_ppriv[i].sets[1] == NULL))
353                 {
354                         return 0;
355                 }
356         }
357         
358         return 1;
359 }
360
361 int sis_final_context(struct drm_device *dev, int context)
362 {
363         int i;
364
365         for (i=0; i<MAX_CONTEXT; i++) {
366                 if (global_ppriv[i].used &&
367                     (global_ppriv[i].context == context))
368                         break;
369         }
370
371         if (i < MAX_CONTEXT) {
372                 set_t *set;
373                 unsigned int item;
374                 int retval;
375
376                 DRM_DEBUG("find socket %d, context = %d\n", i, context);
377
378                 /* Video Memory */
379                 set = global_ppriv[i].sets[0];
380                 retval = setFirst(set, &item);
381                 while (retval) {
382                         DRM_DEBUG("free video memory 0x%x\n", item);
383 #if defined(__linux__) && defined(CONFIG_FB_SIS)
384                         sis_free(item);
385 #else
386                         mmFreeMem((PMemBlock)item);
387 #endif
388                         retval = setNext(set, &item);
389                 }
390                 setDestroy(set);
391
392                 /* AGP Memory */
393                 set = global_ppriv[i].sets[1];
394                 retval = setFirst(set, &item);
395                 while (retval) {
396                         DRM_DEBUG("free agp memory 0x%x\n", item);
397                         mmFreeMem((PMemBlock)item);
398                         retval = setNext(set, &item);
399                 }
400                 setDestroy(set);
401
402                 global_ppriv[i].used = 0;         
403         }
404         
405         return 1;
406 }
407
408 void DRM(driver_register_fns)(drm_device_t *dev)
409 {
410         dev->driver_features = DRIVER_USE_AGP | DRIVER_USE_MTRR;
411         dev->fn_tbl.context_ctor = sis_init_context;
412         dev->fn_tbl.context_dtor = sis_final_context;
413 }