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