2 * Copyright 1998-2003 VIA Technologies, Inc. All Rights Reserved.
3 * Copyright 2001-2003 S3 Graphics, Inc. All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sub license,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * VIA, S3 GRAPHICS, AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
30 #define MAX_CONTEXT 100
32 unsigned int VIA_DEBUG = 1;
37 set_t *sets[2]; /* 0 for frame buffer, 1 for AGP , 2 for System */
40 static via_context_t global_ppriv[MAX_CONTEXT];
42 static int add_alloc_set(int context, int type, unsigned int val)
46 for (i = 0; i < MAX_CONTEXT; i++) {
47 if (global_ppriv[i].used && global_ppriv[i].context == context) {
48 retval = via_setAdd(global_ppriv[i].sets[type], val);
56 static int del_alloc_set(int context, int type, unsigned int val)
60 for (i = 0; i < MAX_CONTEXT; i++)
61 if (global_ppriv[i].used && global_ppriv[i].context == context) {
62 retval = via_setDel(global_ppriv[i].sets[type], val);
69 /* agp memory management */
70 static memHeap_t *AgpHeap = NULL;
72 int via_agp_init(DRM_IOCTL_ARGS)
76 DRM_COPY_FROM_USER_IOCTL(agp, (drm_via_agp_t *) data, sizeof(agp));
78 AgpHeap = via_mmInit(agp.offset, agp.size);
80 DRM_DEBUG("offset = %lu, size = %lu", (unsigned long)agp.offset, (unsigned long)agp.size);
85 /* fb memory management */
86 static memHeap_t *FBHeap = NULL;
88 int via_fb_init(DRM_IOCTL_ARGS)
92 DRM_COPY_FROM_USER_IOCTL(fb, (drm_via_fb_t *) data, sizeof(fb));
94 FBHeap = via_mmInit(fb.offset, fb.size);
96 DRM_DEBUG("offset = %lu, size = %lu", (unsigned long)fb.offset, (unsigned long)fb.size);
101 int via_init_context(struct drm_device *dev, int context)
105 for (i = 0; i < MAX_CONTEXT; i++)
106 if (global_ppriv[i].used &&
107 (global_ppriv[i].context == context))
110 if (i >= MAX_CONTEXT) {
111 for (i = 0; i < MAX_CONTEXT; i++) {
112 if (!global_ppriv[i].used) {
113 global_ppriv[i].context = context;
114 global_ppriv[i].used = 1;
115 global_ppriv[i].sets[0] = via_setInit();
116 global_ppriv[i].sets[1] = via_setInit();
117 DRM_DEBUG("init allocation set, socket=%d,"
118 " context = %d\n", i, context);
123 if ((i >= MAX_CONTEXT) || (global_ppriv[i].sets[0] == NULL) ||
124 (global_ppriv[i].sets[1] == NULL)) {
132 int via_final_context(struct drm_device *dev, int context)
135 for (i = 0; i < MAX_CONTEXT; i++)
136 if (global_ppriv[i].used &&
137 (global_ppriv[i].context == context))
140 if (i < MAX_CONTEXT) {
145 DRM_DEBUG("find socket %d, context = %d\n", i, context);
148 set = global_ppriv[i].sets[0];
149 retval = via_setFirst(set, &item);
151 DRM_DEBUG("free video memory 0x%x\n", item);
152 via_mmFreeMem((PMemBlock) item);
153 retval = via_setNext(set, &item);
158 set = global_ppriv[i].sets[1];
159 retval = via_setFirst(set, &item);
161 DRM_DEBUG("free agp memory 0x%x\n", item);
162 via_mmFreeMem((PMemBlock) item);
163 retval = via_setNext(set, &item);
167 global_ppriv[i].used = 0;
169 #if defined(__linux__)
170 /* Linux specific until context tracking code gets ported to BSD */
171 /* Last context, perform cleanup */
172 if (dev->ctx_count == 1 && dev->dev_private) {
173 DRM_DEBUG("Last Context\n");
175 drm_irq_uninstall(dev);
177 via_do_cleanup_map(dev);
184 int via_mem_alloc(DRM_IOCTL_ARGS)
188 DRM_COPY_FROM_USER_IOCTL(mem, (drm_via_mem_t *) data, sizeof(mem));
191 if (via_fb_alloc(&mem) < 0)
193 DRM_COPY_TO_USER_IOCTL((drm_via_mem_t *) data, mem,
197 if (via_agp_alloc(&mem) < 0)
199 DRM_COPY_TO_USER_IOCTL((drm_via_mem_t *) data, mem,
207 int via_fb_alloc(drm_via_mem_t * mem)
217 fb.context = mem->context;
219 block = via_mmAllocMem(FBHeap, fb.size, 5, 0);
221 fb.offset = block->ofs;
222 fb.free = (unsigned int)block;
223 if (!add_alloc_set(fb.context, VIDEO, fb.free)) {
224 DRM_DEBUG("adding to allocation set fails\n");
225 via_mmFreeMem((PMemBlock) fb.free);
235 mem->offset = fb.offset;
236 mem->index = fb.free;
238 DRM_DEBUG("alloc fb, size = %d, offset = %d\n", fb.size,
244 int via_agp_alloc(drm_via_mem_t * mem)
253 agp.size = mem->size;
254 agp.context = mem->context;
256 block = via_mmAllocMem(AgpHeap, agp.size, 5, 0);
258 agp.offset = block->ofs;
259 agp.free = (unsigned int)block;
260 if (!add_alloc_set(agp.context, AGP, agp.free)) {
261 DRM_DEBUG("adding to allocation set fails\n");
262 via_mmFreeMem((PMemBlock) agp.free);
271 mem->offset = agp.offset;
272 mem->index = agp.free;
274 DRM_DEBUG("alloc agp, size = %d, offset = %d\n", agp.size,
275 (unsigned int)agp.offset);
279 int via_mem_free(DRM_IOCTL_ARGS)
283 DRM_COPY_FROM_USER_IOCTL(mem, (drm_via_mem_t *) data, sizeof(mem));
288 if (via_fb_free(&mem) == 0)
292 if (via_agp_free(&mem) == 0)
300 int via_fb_free(drm_via_mem_t * mem)
309 fb.free = mem->index;
310 fb.context = mem->context;
317 via_mmFreeMem((PMemBlock) fb.free);
319 if (!del_alloc_set(fb.context, VIDEO, fb.free)) {
323 DRM_DEBUG("free fb, free = %d\n", fb.free);
328 int via_agp_free(drm_via_mem_t * mem)
334 agp.free = mem->index;
335 agp.context = mem->context;
340 via_mmFreeMem((PMemBlock) agp.free);
342 if (!del_alloc_set(agp.context, AGP, agp.free)) {
346 DRM_DEBUG("free agp, free = %d\n", agp.free);