2 * Copyright 1998-2003 VIA Technologies, Inc. All Rights Reserved.
3 * Copyright 2001-2003 S3 Graphics, Inc. All Rights Reserved.
4 * Copyright 2000 Silicon Integrated Systems Corp, Inc., HsinChu, Taiwan.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sub license,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
20 * VIA, S3 GRAPHICS, AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
25 #include <linux/module.h>
26 #include <linux/delay.h>
27 #include <linux/errno.h>
28 #include <linux/kernel.h>
29 #include <linux/slab.h>
30 #include <linux/poll.h>
31 #include <linux/pci.h>
35 extern unsigned int VIA_DEBUG;
37 set_t *via_setInit(void)
41 set = (set_t *) drm_alloc(sizeof(set_t), DRM_MEM_DRIVER);
42 for (i = 0; i < SET_SIZE; i++) {
43 set->list[i].free_next = i + 1;
44 set->list[i].alloc_next = -1;
46 set->list[SET_SIZE - 1].free_next = -1;
53 int via_setAdd(set_t * set, ITEM_TYPE item)
57 set->list[free].val = item;
58 set->free = set->list[free].free_next;
62 set->list[free].alloc_next = set->alloc;
64 set->list[free].free_next = -1;
68 int via_setDel(set_t * set, ITEM_TYPE item)
70 int alloc = set->alloc;
74 if (set->list[alloc].val == item) {
76 set->list[prev].alloc_next =
77 set->list[alloc].alloc_next;
79 set->alloc = set->list[alloc].alloc_next;
83 alloc = set->list[alloc].alloc_next;
89 set->list[alloc].free_next = set->free;
91 set->list[alloc].alloc_next = -1;
96 /* setFirst -> setAdd -> setNext is wrong */
98 int via_setFirst(set_t * set, ITEM_TYPE * item)
100 if (set->alloc == -1)
103 *item = set->list[set->alloc].val;
104 set->trace = set->list[set->alloc].alloc_next;
109 int via_setNext(set_t * set, ITEM_TYPE * item)
111 if (set->trace == -1)
114 *item = set->list[set->trace].val;
115 set->trace = set->list[set->trace].alloc_next;
120 int via_setDestroy(set_t * set)
122 drm_free(set, sizeof(set_t), DRM_MEM_DRIVER);
127 #define ISFREE(bptr) ((bptr)->free)
129 #define fprintf(fmt, arg...) do{}while(0)
131 memHeap_t *via_mmInit(int ofs, int size)
138 blocks = (TMemBlock *) drm_calloc(1, sizeof(TMemBlock), DRM_MEM_DRIVER);
144 return (memHeap_t *) blocks;
149 static TMemBlock *SliceBlock(TMemBlock * p,
150 int startofs, int size,
151 int reserved, int alignment)
156 if (startofs > p->ofs) {
158 (TMemBlock *) drm_calloc(1, sizeof(TMemBlock),
160 newblock->ofs = startofs;
161 newblock->size = p->size - (startofs - p->ofs);
163 newblock->next = p->next;
164 p->size -= newblock->size;
170 if (size < p->size) {
172 (TMemBlock *) drm_calloc(1, sizeof(TMemBlock),
174 newblock->ofs = startofs + size;
175 newblock->size = p->size - size;
177 newblock->next = p->next;
182 /* p = middle block */
183 p->align = alignment;
185 p->reserved = reserved;
189 PMemBlock via_mmAllocMem(memHeap_t * heap, int size, int align2,
192 int mask, startofs, endofs;
195 if (!heap || align2 < 0 || size <= 0)
198 mask = (1 << align2) - 1;
200 p = (TMemBlock *) heap;
204 startofs = (p->ofs + mask) & ~mask;
206 if (startofs < startSearch)
207 startofs = startSearch;
209 endofs = startofs + size;
211 if (endofs <= (p->ofs + p->size))
221 p = SliceBlock(p, startofs, size, 0, mask + 1);
227 static __inline__ int Join2Blocks(TMemBlock * p)
229 if (p->free && p->next && p->next->free) {
230 TMemBlock *q = p->next;
233 drm_free(q, sizeof(TMemBlock), DRM_MEM_DRIVER);
241 int via_mmFreeMem(PMemBlock b)
249 fprintf(stderr, "no heap\n");
257 while (p && p != b) {
262 if (!p || p->free || p->reserved) {
264 fprintf(stderr, "block not found in heap\n");
266 fprintf(stderr, "block already free\n");
268 fprintf(stderr, "block is reserved\n");