sis_ds.c revision 145132
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 * $FreeBSD: head/sys/dev/drm/sis_ds.c 145132 2005-04-16 03:44:47Z anholt $
30 */
31
32#include "dev/drm/drmP.h"
33#include "dev/drm/drm.h"
34#include "dev/drm/sis_ds.h"
35
36/* Set Data Structure, not check repeated value
37 * temporarily used
38 */
39
40set_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
59int 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
77int 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
107int 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
118int 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
129int 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 Wittawat Yamwong
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
162memHeap_t *mmInit(int ofs, 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' */
180int 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
197static TMemBlock *SliceBlock(TMemBlock * p,
198			     int startofs, int size,
199			     int reserved, int alignment)
200{
201	TMemBlock *newblock;
202
203	/* break left */
204	if (startofs > p->ofs) {
205		newblock = (TMemBlock *) drm_calloc(1, sizeof(TMemBlock),
206						    DRM_MEM_DRIVER);
207		newblock->ofs = startofs;
208		newblock->size = p->size - (startofs - p->ofs);
209		newblock->free = 1;
210		newblock->next = p->next;
211		p->size -= newblock->size;
212		p->next = newblock;
213		p = newblock;
214	}
215
216	/* break right */
217	if (size < p->size) {
218		newblock = (TMemBlock *) drm_calloc(1, sizeof(TMemBlock),
219						    DRM_MEM_DRIVER);
220		newblock->ofs = startofs + size;
221		newblock->size = p->size - size;
222		newblock->free = 1;
223		newblock->next = p->next;
224		p->size = size;
225		p->next = newblock;
226	}
227
228	/* p = middle block */
229	p->align = alignment;
230	p->free = 0;
231	p->reserved = reserved;
232	return p;
233}
234
235PMemBlock mmAllocMem(memHeap_t * heap, int size, int align2, int startSearch)
236{
237	int mask, startofs, endofs;
238	TMemBlock *p;
239
240	if (heap == NULL || align2 < 0 || size <= 0)
241		return NULL;
242
243	mask = (1 << align2) - 1;
244	startofs = 0;
245	p = (TMemBlock *) heap;
246	while (p != NULL) {
247		if (ISFREE(p)) {
248			startofs = (p->ofs + mask) & ~mask;
249			if (startofs < startSearch) {
250				startofs = startSearch;
251			}
252			endofs = startofs + size;
253			if (endofs <= (p->ofs + p->size))
254				break;
255		}
256		p = p->next;
257	}
258	if (p == NULL)
259		return NULL;
260	p = SliceBlock(p, startofs, size, 0, mask + 1);
261	p->heap = heap;
262	return p;
263}
264
265static __inline__ int Join2Blocks(TMemBlock * p)
266{
267	if (p->free && p->next && p->next->free) {
268		TMemBlock *q = p->next;
269		p->size += q->size;
270		p->next = q->next;
271		drm_free(q, sizeof(TMemBlock), DRM_MEM_DRIVER);
272		return 1;
273	}
274	return 0;
275}
276
277int mmFreeMem(PMemBlock b)
278{
279	TMemBlock *p, *prev;
280
281	if (b == NULL)
282		return 0;
283	if (b->heap == NULL)
284		return -1;
285
286	p = b->heap;
287	prev = NULL;
288	while (p != NULL && p != b) {
289		prev = p;
290		p = p->next;
291	}
292	if (p == NULL || p->free || p->reserved)
293		return -1;
294
295	p->free = 1;
296	Join2Blocks(p);
297	if (prev)
298		Join2Blocks(prev);
299	return 0;
300}
301