drm: Use an include directory hierarchy similar to the Linux one
[dragonfly.git] / sys / dev / drm / sis / 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 <drm/drmP.h>
32 #include "sis_ds.h"
33
34 /* Set Data Structure, not check repeated value
35  * temporarily used
36  */
37
38 set_t *setInit(void)
39 {
40         int i;
41         set_t *set;
42
43         set = (set_t *) drm_alloc(sizeof(set_t), DRM_MEM_DRIVER);
44         if (set != NULL) {
45                 for (i = 0; i < SET_SIZE; i++) {
46                         set->list[i].free_next = i + 1;
47                         set->list[i].alloc_next = -1;
48                 }
49                 set->list[SET_SIZE - 1].free_next = -1;
50                 set->free = 0;
51                 set->alloc = -1;
52                 set->trace = -1;
53         }
54         return set;
55 }
56
57 int setAdd(set_t * set, ITEM_TYPE item)
58 {
59         int free = set->free;
60
61         if (free != -1) {
62                 set->list[free].val = item;
63                 set->free = set->list[free].free_next;
64         } else {
65                 return 0;
66         }
67
68         set->list[free].alloc_next = set->alloc;
69         set->alloc = free;
70         set->list[free].free_next = -1;
71
72         return 1;
73 }
74
75 int setDel(set_t * set, ITEM_TYPE item)
76 {
77         int alloc = set->alloc;
78         int prev = -1;
79
80         while (alloc != -1) {
81                 if (set->list[alloc].val == item) {
82                         if (prev != -1)
83                                 set->list[prev].alloc_next =
84                                     set->list[alloc].alloc_next;
85                         else
86                                 set->alloc = set->list[alloc].alloc_next;
87                         break;
88                 }
89                 prev = alloc;
90                 alloc = set->list[alloc].alloc_next;
91         }
92
93         if (alloc == -1)
94                 return 0;
95
96         set->list[alloc].free_next = set->free;
97         set->free = alloc;
98         set->list[alloc].alloc_next = -1;
99
100         return 1;
101 }
102
103 /* setFirst -> setAdd -> setNext is wrong */
104
105 int setFirst(set_t * set, ITEM_TYPE * item)
106 {
107         if (set->alloc == -1)
108                 return 0;
109
110         *item = set->list[set->alloc].val;
111         set->trace = set->list[set->alloc].alloc_next;
112
113         return 1;
114 }
115
116 int setNext(set_t * set, ITEM_TYPE * item)
117 {
118         if (set->trace == -1)
119                 return 0;
120
121         *item = set->list[set->trace].val;
122         set->trace = set->list[set->trace].alloc_next;
123
124         return 1;
125 }
126
127 int setDestroy(set_t * set)
128 {
129         drm_free(set, DRM_MEM_DRIVER);
130
131         return 1;
132 }
133
134 /*
135  * GLX Hardware Device Driver common code
136  * Copyright (C) 1999 Wittawat Yamwong
137  *
138  * Permission is hereby granted, free of charge, to any person obtaining a
139  * copy of this software and associated documentation files (the "Software"),
140  * to deal in the Software without restriction, including without limitation
141  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
142  * and/or sell copies of the Software, and to permit persons to whom the
143  * Software is furnished to do so, subject to the following conditions:
144  *
145  * The above copyright notice and this permission notice shall be included
146  * in all copies or substantial portions of the Software.
147  *
148  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
149  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
150  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
151  * WITTAWAT YAMWONG, OR ANY OTHER CONTRIBUTORS BE LIABLE FOR ANY CLAIM,
152  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
153  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
154  * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
155  *
156  */
157
158 #define ISFREE(bptr) ((bptr)->free)
159
160 memHeap_t *mmInit(int ofs, int size)
161 {
162         PMemBlock blocks;
163
164         if (size <= 0)
165                 return NULL;
166
167         blocks = (TMemBlock *) drm_calloc(1, sizeof(TMemBlock), DRM_MEM_DRIVER);
168         if (blocks != NULL) {
169                 blocks->ofs = ofs;
170                 blocks->size = size;
171                 blocks->free = 1;
172                 return (memHeap_t *) blocks;
173         } else
174                 return NULL;
175 }
176
177 /* Checks if a pointer 'b' is part of the heap 'heap' */
178 int mmBlockInHeap(memHeap_t * heap, PMemBlock b)
179 {
180         TMemBlock *p;
181
182         if (heap == NULL || b == NULL)
183                 return 0;
184
185         p = heap;
186         while (p != NULL && p != b) {
187                 p = p->next;
188         }
189         if (p == b)
190                 return 1;
191         else
192                 return 0;
193 }
194
195 static TMemBlock *SliceBlock(TMemBlock * p,
196                              int startofs, int size,
197                              int reserved, int alignment)
198 {
199         TMemBlock *newblock;
200
201         /* break left */
202         if (startofs > p->ofs) {
203                 newblock = (TMemBlock *) drm_calloc(1, sizeof(TMemBlock),
204                                                     DRM_MEM_DRIVER);
205                 newblock->ofs = startofs;
206                 newblock->size = p->size - (startofs - p->ofs);
207                 newblock->free = 1;
208                 newblock->next = p->next;
209                 p->size -= newblock->size;
210                 p->next = newblock;
211                 p = newblock;
212         }
213
214         /* break right */
215         if (size < p->size) {
216                 newblock = (TMemBlock *) drm_calloc(1, sizeof(TMemBlock),
217                                                     DRM_MEM_DRIVER);
218                 newblock->ofs = startofs + size;
219                 newblock->size = p->size - size;
220                 newblock->free = 1;
221                 newblock->next = p->next;
222                 p->size = size;
223                 p->next = newblock;
224         }
225
226         /* p = middle block */
227         p->align = alignment;
228         p->free = 0;
229         p->reserved = reserved;
230         return p;
231 }
232
233 PMemBlock mmAllocMem(memHeap_t * heap, int size, int align2, int startSearch)
234 {
235         int mask, startofs, endofs;
236         TMemBlock *p;
237
238         if (heap == NULL || align2 < 0 || size <= 0)
239                 return NULL;
240
241         mask = (1 << align2) - 1;
242         startofs = 0;
243         p = (TMemBlock *) heap;
244         while (p != NULL) {
245                 if (ISFREE(p)) {
246                         startofs = (p->ofs + mask) & ~mask;
247                         if (startofs < startSearch) {
248                                 startofs = startSearch;
249                         }
250                         endofs = startofs + size;
251                         if (endofs <= (p->ofs + p->size))
252                                 break;
253                 }
254                 p = p->next;
255         }
256         if (p == NULL)
257                 return NULL;
258         p = SliceBlock(p, startofs, size, 0, mask + 1);
259         p->heap = heap;
260         return p;
261 }
262
263 static __inline__ int Join2Blocks(TMemBlock * p)
264 {
265         if (p->free && p->next && p->next->free) {
266                 TMemBlock *q = p->next;
267                 p->size += q->size;
268                 p->next = q->next;
269                 drm_free(q, DRM_MEM_DRIVER);
270                 return 1;
271         }
272         return 0;
273 }
274
275 int mmFreeMem(PMemBlock b)
276 {
277         TMemBlock *p, *prev;
278
279         if (b == NULL)
280                 return 0;
281         if (b->heap == NULL)
282                 return -1;
283
284         p = b->heap;
285         prev = NULL;
286         while (p != NULL && p != b) {
287                 prev = p;
288                 p = p->next;
289         }
290         if (p == NULL || p->free || p->reserved)
291                 return -1;
292
293         p->free = 1;
294         Join2Blocks(p);
295         if (prev)
296                 Join2Blocks(prev);
297         return 0;
298 }