vserver 1.9.5.x5
[linux-2.6.git] / drivers / char / drm / sis_ds.c
1 /* sis_ds.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 "drm.h"
33 #include "sis_ds.h"
34
35 /* Set Data Structure, not check repeated value
36  * temporarily used
37  */
38
39 set_t *setInit(void)
40 {
41         int i;
42         set_t *set;
43
44         set = (set_t *)drm_alloc(sizeof(set_t), DRM_MEM_DRIVER);
45         if (set != NULL) {
46                 for (i = 0; i < SET_SIZE; i++) {
47                         set->list[i].free_next = i + 1;    
48                         set->list[i].alloc_next = -1;
49                 }
50                 set->list[SET_SIZE-1].free_next = -1;
51                 set->free = 0;
52                 set->alloc = -1;
53                 set->trace = -1;
54         }
55         return set;
56 }
57
58 int setAdd(set_t *set, ITEM_TYPE item)
59 {
60         int free = set->free;
61   
62         if (free != -1) {
63                 set->list[free].val = item;
64                 set->free = set->list[free].free_next;
65         } else {
66                 return 0;
67         }
68
69         set->list[free].alloc_next = set->alloc;
70         set->alloc = free;  
71         set->list[free].free_next = -1;    
72
73         return 1;
74 }
75
76 int setDel(set_t *set, ITEM_TYPE item)
77 {
78         int alloc = set->alloc;
79         int prev = -1;  
80
81         while (alloc != -1) {
82                 if (set->list[alloc].val == item) {
83                         if (prev != -1)
84                                 set->list[prev].alloc_next =
85                                     set->list[alloc].alloc_next;
86                         else
87                                 set->alloc = set->list[alloc].alloc_next;
88                         break;
89                 }
90                 prev = alloc;
91                 alloc = set->list[alloc].alloc_next;
92         }
93
94         if (alloc == -1)
95                 return 0;
96
97         set->list[alloc].free_next = set->free;
98         set->free = alloc;
99         set->list[alloc].alloc_next = -1;
100
101         return 1;
102 }
103
104 /* setFirst -> setAdd -> setNext is wrong */
105
106 int setFirst(set_t *set, ITEM_TYPE *item)
107 {
108         if (set->alloc == -1)
109                 return 0;
110
111         *item = set->list[set->alloc].val;
112         set->trace = set->list[set->alloc].alloc_next;
113
114         return 1;
115 }
116
117 int setNext(set_t *set, ITEM_TYPE *item)
118 {
119         if (set->trace == -1)
120                 return 0;
121
122         *item = set->list[set->trace].val;
123         set->trace = set->list[set->trace].alloc_next;
124
125         return 1;
126 }
127
128 int setDestroy(set_t *set)
129 {
130         drm_free(set, sizeof(set_t), DRM_MEM_DRIVER);
131
132         return 1;
133 }
134
135 /*
136  * GLX Hardware Device Driver common code
137  * Copyright (C) 1999 Wittawat Yamwong
138  *
139  * Permission is hereby granted, free of charge, to any person obtaining a
140  * copy of this software and associated documentation files (the "Software"),
141  * to deal in the Software without restriction, including without limitation
142  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
143  * and/or sell copies of the Software, and to permit persons to whom the
144  * Software is furnished to do so, subject to the following conditions:
145  *
146  * The above copyright notice and this permission notice shall be included
147  * in all copies or substantial portions of the Software.
148  *
149  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
150  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
151  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
152  * WITTAWAT YAMWONG, OR ANY OTHER CONTRIBUTORS BE LIABLE FOR ANY CLAIM, 
153  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 
154  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 
155  * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
156  *
157  */
158
159 #define ISFREE(bptr) ((bptr)->free)
160
161 memHeap_t *mmInit(int ofs,
162                   int size)
163 {
164         PMemBlock blocks;
165
166         if (size <= 0)
167                 return NULL;
168
169         blocks = (TMemBlock *)drm_calloc(1, sizeof(TMemBlock), DRM_MEM_DRIVER);
170         if (blocks != NULL) {
171                 blocks->ofs = ofs;
172                 blocks->size = size;
173                 blocks->free = 1;
174                 return (memHeap_t *)blocks;
175         } else
176                 return NULL;
177 }
178
179 /* Checks if a pointer 'b' is part of the heap 'heap' */
180 int mmBlockInHeap(memHeap_t *heap, PMemBlock b)
181 {
182         TMemBlock *p;
183
184         if (heap == NULL || b == NULL)
185                 return 0;
186
187         p = heap;
188         while (p != NULL && p != b) {
189                 p = p->next;
190         }
191         if (p == b)
192                 return 1;
193         else
194                 return 0;
195 }
196
197 /* Kludgey workaround for existing i810 server.  Remove soon.
198  */
199 memHeap_t *mmAddRange( memHeap_t *heap,
200                        int ofs,
201                        int size )
202 {
203         PMemBlock blocks;
204         blocks = (TMemBlock *)drm_calloc(2, sizeof(TMemBlock), DRM_MEM_DRIVER);
205         if (blocks != NULL) {
206                 blocks[0].size = size;
207                 blocks[0].free = 1;
208                 blocks[0].ofs = ofs;
209                 blocks[0].next = &blocks[1];
210
211                 /* Discontinuity - stops JoinBlock from trying to join
212                  * non-adjacent ranges.
213                  */
214                 blocks[1].size = 0;
215                 blocks[1].free = 0;
216                 blocks[1].ofs = ofs+size;
217                 blocks[1].next = (PMemBlock)heap;
218                 return (memHeap_t *)blocks;
219         } else
220                 return heap;
221 }
222
223 static TMemBlock* SliceBlock(TMemBlock *p, 
224                              int startofs, int size, 
225                              int reserved, int alignment)
226 {
227         TMemBlock *newblock;
228
229         /* break left */
230         if (startofs > p->ofs) {
231                 newblock = (TMemBlock*) drm_calloc(1, sizeof(TMemBlock),
232                     DRM_MEM_DRIVER);
233                 newblock->ofs = startofs;
234                 newblock->size = p->size - (startofs - p->ofs);
235                 newblock->free = 1;
236                 newblock->next = p->next;
237                 p->size -= newblock->size;
238                 p->next = newblock;
239                 p = newblock;
240         }
241
242         /* break right */
243         if (size < p->size) {
244                 newblock = (TMemBlock*) drm_calloc(1, sizeof(TMemBlock),
245                     DRM_MEM_DRIVER);
246                 newblock->ofs = startofs + size;
247                 newblock->size = p->size - size;
248                 newblock->free = 1;
249                 newblock->next = p->next;
250                 p->size = size;
251                 p->next = newblock;
252         }
253
254         /* p = middle block */
255         p->align = alignment;
256         p->free = 0;
257         p->reserved = reserved;
258         return p;
259 }
260
261 PMemBlock mmAllocMem( memHeap_t *heap, int size, int align2, int startSearch)
262 {
263         int mask,startofs, endofs;
264         TMemBlock *p;
265         
266         if (heap == NULL || align2 < 0 || size <= 0)
267                 return NULL;
268
269         mask = (1 << align2)-1;
270         startofs = 0;
271         p = (TMemBlock *)heap;
272         while (p != NULL) {
273                 if (ISFREE(p)) {
274                         startofs = (p->ofs + mask) & ~mask;
275                         if ( startofs < startSearch ) {
276                                 startofs = startSearch;
277                         }
278                         endofs = startofs+size;
279                         if (endofs <= (p->ofs+p->size))
280                                 break;
281                 }
282                 p = p->next;
283         }
284         if (p == NULL)
285                 return NULL;
286         p = SliceBlock(p,startofs,size,0,mask+1);
287         p->heap = heap;
288         return p;
289 }
290
291 static __inline__ int Join2Blocks(TMemBlock *p)
292 {
293         if (p->free && p->next && p->next->free) {
294                 TMemBlock *q = p->next;
295                 p->size += q->size;
296                 p->next = q->next;
297                 drm_free(q, sizeof(TMemBlock), DRM_MEM_DRIVER);
298                 return 1;
299         }
300         return 0;
301 }
302
303 int mmFreeMem(PMemBlock b)
304 {
305         TMemBlock *p, *prev;
306
307         if (b == NULL)
308                 return 0;
309         if (b->heap == NULL)
310                 return -1;
311
312         p = b->heap;
313         prev = NULL;
314         while (p != NULL && p != b) {
315                 prev = p;
316                 p = p->next;
317         }
318         if (p == NULL || p->free || p->reserved)
319                 return -1;
320
321         p->free = 1;
322         Join2Blocks(p);
323         if (prev)
324         Join2Blocks(prev);
325         return 0;
326 }
327
328 int mmReserveMem(memHeap_t *heap, int offset,int size)
329 {
330         int endofs;
331         TMemBlock *p;
332
333         if (heap == NULL || size <= 0)
334                 return -1;
335
336         endofs = offset + size;
337         p = (TMemBlock *)heap;
338         while (p && p->ofs <= offset) {
339                 if (ISFREE(p) && endofs <= (p->ofs+p->size)) {
340                         SliceBlock(p,offset,size,1,1);
341                         return 0;
342                 }
343                 p = p->next;
344         }
345         return -1;
346 }
347
348 int mmFreeReserved(memHeap_t *heap, int offset)
349 {
350         TMemBlock *p,*prev;
351
352         if (heap == NULL)
353                 return -1;
354
355         p = (TMemBlock *)heap;
356         prev = NULL;
357         while (p != NULL && p->ofs != offset) {
358                 prev = p;
359                 p = p->next;
360         }
361         if (p == NULL || !p->reserved)
362                 return -1;
363
364         p->free = 1;
365         p->reserved = 0;
366         Join2Blocks(p);
367         if (prev != NULL)
368                 Join2Blocks(prev);
369         return 0;
370 }
371
372 void mmDestroy(memHeap_t *heap)
373 {
374         TMemBlock *p,*q;
375
376         if (heap == NULL)
377                 return;
378
379         p = (TMemBlock *)heap;
380         while (p != NULL) {
381                 q = p->next;
382                 drm_free(p, sizeof(TMemBlock), DRM_MEM_DRIVER);
383                 p = q;
384         }
385 }