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