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
35 set_t *sets[2]; /* 0 for frame buffer, 1 for AGP , 2 for System */
38 static via_context_t global_ppriv[MAX_CONTEXT];
40 static int via_agp_alloc(drm_via_mem_t * mem);
41 static int via_agp_free(drm_via_mem_t * mem);
42 static int via_fb_alloc(drm_via_mem_t * mem);
43 static int via_fb_free(drm_via_mem_t * mem);
45 static int add_alloc_set(int context, int type, unsigned int val)
49 for (i = 0; i < MAX_CONTEXT; i++) {
50 if (global_ppriv[i].used && global_ppriv[i].context == context) {
51 retval = via_setAdd(global_ppriv[i].sets[type], val);
59 static int del_alloc_set(int context, int type, unsigned int val)
63 for (i = 0; i < MAX_CONTEXT; i++)
64 if (global_ppriv[i].used && global_ppriv[i].context == context) {
65 retval = via_setDel(global_ppriv[i].sets[type], val);
72 /* agp memory management */
73 static memHeap_t *AgpHeap = NULL;
75 int via_agp_init(DRM_IOCTL_ARGS)
79 DRM_COPY_FROM_USER_IOCTL(agp, (drm_via_agp_t __user *) data,
82 AgpHeap = via_mmInit(agp.offset, agp.size);
84 DRM_DEBUG("offset = %lu, size = %lu", (unsigned long)agp.offset,
85 (unsigned long)agp.size);
90 /* fb memory management */
91 static memHeap_t *FBHeap = NULL;
93 int via_fb_init(DRM_IOCTL_ARGS)
97 DRM_COPY_FROM_USER_IOCTL(fb, (drm_via_fb_t __user *) data, sizeof(fb));
99 FBHeap = via_mmInit(fb.offset, fb.size);
101 DRM_DEBUG("offset = %lu, size = %lu", (unsigned long)fb.offset,
102 (unsigned long)fb.size);
107 int via_init_context(struct drm_device *dev, int context)
111 for (i = 0; i < MAX_CONTEXT; i++)
112 if (global_ppriv[i].used &&
113 (global_ppriv[i].context == context))
116 if (i >= MAX_CONTEXT) {
117 for (i = 0; i < MAX_CONTEXT; i++) {
118 if (!global_ppriv[i].used) {
119 global_ppriv[i].context = context;
120 global_ppriv[i].used = 1;
121 global_ppriv[i].sets[0] = via_setInit();
122 global_ppriv[i].sets[1] = via_setInit();
123 DRM_DEBUG("init allocation set, socket=%d,"
124 " context = %d\n", i, context);
129 if ((i >= MAX_CONTEXT) || (global_ppriv[i].sets[0] == NULL) ||
130 (global_ppriv[i].sets[1] == NULL)) {
138 int via_final_context(struct drm_device *dev, int context)
141 drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
143 for (i = 0; i < MAX_CONTEXT; i++)
144 if (global_ppriv[i].used &&
145 (global_ppriv[i].context == context))
148 if (i < MAX_CONTEXT) {
153 DRM_DEBUG("find socket %d, context = %d\n", i, context);
156 set = global_ppriv[i].sets[0];
157 retval = via_setFirst(set, &item);
159 DRM_DEBUG("free video memory 0x%lx\n", item);
160 via_mmFreeMem((PMemBlock) item);
161 retval = via_setNext(set, &item);
166 set = global_ppriv[i].sets[1];
167 retval = via_setFirst(set, &item);
169 DRM_DEBUG("free agp memory 0x%lx\n", item);
170 via_mmFreeMem((PMemBlock) item);
171 retval = via_setNext(set, &item);
174 global_ppriv[i].used = 0;
176 via_release_futex(dev_priv, context);
178 #if defined(__linux__)
179 /* Linux specific until context tracking code gets ported to BSD */
180 /* Last context, perform cleanup */
181 if (dev->ctx_count == 1 && dev->dev_private) {
182 DRM_DEBUG("Last Context\n");
184 drm_irq_uninstall(dev);
186 via_cleanup_futex(dev_priv);
187 via_do_cleanup_map(dev);
194 int via_mem_alloc(DRM_IOCTL_ARGS)
198 DRM_COPY_FROM_USER_IOCTL(mem, (drm_via_mem_t __user *) data,
203 if (via_fb_alloc(&mem) < 0)
205 DRM_COPY_TO_USER_IOCTL((drm_via_mem_t __user *) data, mem,
209 if (via_agp_alloc(&mem) < 0)
211 DRM_COPY_TO_USER_IOCTL((drm_via_mem_t __user *) data, mem,
219 static int via_fb_alloc(drm_via_mem_t * mem)
229 fb.context = mem->context;
231 block = via_mmAllocMem(FBHeap, fb.size, 5, 0);
233 fb.offset = block->ofs;
234 fb.free = (unsigned long)block;
235 if (!add_alloc_set(fb.context, VIDEO, fb.free)) {
236 DRM_DEBUG("adding to allocation set fails\n");
237 via_mmFreeMem((PMemBlock) fb.free);
247 mem->offset = fb.offset;
248 mem->index = fb.free;
250 DRM_DEBUG("alloc fb, size = %d, offset = %d\n", fb.size,
256 static int via_agp_alloc(drm_via_mem_t * mem)
265 agp.size = mem->size;
266 agp.context = mem->context;
268 block = via_mmAllocMem(AgpHeap, agp.size, 5, 0);
270 agp.offset = block->ofs;
271 agp.free = (unsigned long)block;
272 if (!add_alloc_set(agp.context, AGP, agp.free)) {
273 DRM_DEBUG("adding to allocation set fails\n");
274 via_mmFreeMem((PMemBlock) agp.free);
283 mem->offset = agp.offset;
284 mem->index = agp.free;
286 DRM_DEBUG("alloc agp, size = %d, offset = %d\n", agp.size,
287 (unsigned int)agp.offset);
291 int via_mem_free(DRM_IOCTL_ARGS)
295 DRM_COPY_FROM_USER_IOCTL(mem, (drm_via_mem_t __user *) data,
301 if (via_fb_free(&mem) == 0)
305 if (via_agp_free(&mem) == 0)
313 static int via_fb_free(drm_via_mem_t * mem)
322 fb.free = mem->index;
323 fb.context = mem->context;
330 via_mmFreeMem((PMemBlock) fb.free);
332 if (!del_alloc_set(fb.context, VIDEO, fb.free)) {
336 DRM_DEBUG("free fb, free = %ld\n", fb.free);
341 static int via_agp_free(drm_via_mem_t * mem)
347 agp.free = mem->index;
348 agp.context = mem->context;
353 via_mmFreeMem((PMemBlock) agp.free);
355 if (!del_alloc_set(agp.context, AGP, agp.free)) {
359 DRM_DEBUG("free agp, free = %ld\n", agp.free);