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 &&
48 global_ppriv[i].context == context) {
49 retval = via_setAdd(global_ppriv[i].sets[type], val);
57 static int del_alloc_set(int context, int type, unsigned int val)
61 for (i = 0; i < MAX_CONTEXT; i++)
62 if (global_ppriv[i].used &&
63 global_ppriv[i].context == context) {
64 retval = via_setDel(global_ppriv[i].sets[type], val);
71 /* agp memory management */
72 static memHeap_t *AgpHeap = NULL;
74 int via_agp_init( DRM_IOCTL_ARGS )
78 DRM_COPY_FROM_USER_IOCTL(agp, (drm_via_agp_t *)data, sizeof(agp));
80 AgpHeap = via_mmInit(agp.offset, agp.size);
82 DRM_DEBUG("offset = %u, size = %u", agp.offset, agp.size);
87 /* fb memory management */
88 static memHeap_t *FBHeap = NULL;
90 int via_fb_init( DRM_IOCTL_ARGS )
95 DRM_COPY_FROM_USER_IOCTL(fb, (drm_via_fb_t *)data, sizeof(fb));
97 FBHeap = via_mmInit(fb.offset, fb.size);
99 DRM_DEBUG("offset = %u, size = %u", fb.offset, fb.size);
104 int via_init_context(struct drm_device *dev, int context)
108 for (i = 0; i < MAX_CONTEXT ; i++)
109 if (global_ppriv[i].used &&
110 (global_ppriv[i].context == context))
113 if (i >= MAX_CONTEXT) {
114 for (i = 0; i < MAX_CONTEXT ; i++) {
115 if (!global_ppriv[i].used) {
116 global_ppriv[i].context = context;
117 global_ppriv[i].used = 1;
118 global_ppriv[i].sets[0] = via_setInit();
119 global_ppriv[i].sets[1] = via_setInit();
120 DRM_DEBUG("init allocation set, socket=%d,"
121 " context = %d\n", i, context);
126 if ((i >= MAX_CONTEXT) || (global_ppriv[i].sets[0] == NULL) ||
127 (global_ppriv[i].sets[1] == NULL)) {
135 int via_final_context(struct drm_device *dev, int context)
138 for (i=0; i<MAX_CONTEXT; i++)
139 if (global_ppriv[i].used &&
140 (global_ppriv[i].context == context))
143 if (i < MAX_CONTEXT) {
148 DRM_DEBUG("find socket %d, context = %d\n", i, context);
151 set = global_ppriv[i].sets[0];
152 retval = via_setFirst(set, &item);
154 DRM_DEBUG("free video memory 0x%x\n", item);
155 via_mmFreeMem((PMemBlock)item);
156 retval = via_setNext(set, &item);
161 set = global_ppriv[i].sets[1];
162 retval = via_setFirst(set, &item);
164 DRM_DEBUG("free agp memory 0x%x\n", item);
165 via_mmFreeMem((PMemBlock)item);
166 retval = via_setNext(set, &item);
170 global_ppriv[i].used = 0;
173 #if defined(__linux__)
174 /* Linux specific until context tracking code gets ported to BSD */
175 /* Last context, perform cleanup */
176 if (dev->ctx_count == 1 && dev->dev_private) {
177 if (dev->irq) drm_irq_uninstall(dev);
179 via_do_cleanup_map(dev);
185 int via_mem_alloc( DRM_IOCTL_ARGS)
189 DRM_COPY_FROM_USER_IOCTL(mem, (drm_via_mem_t *)data, sizeof(mem));
192 if (via_fb_alloc(&mem) < 0)
194 DRM_COPY_TO_USER_IOCTL((drm_via_mem_t *)data, mem,
198 if (via_agp_alloc(&mem) < 0)
200 DRM_COPY_TO_USER_IOCTL((drm_via_mem_t *)data, mem,
208 int via_fb_alloc(drm_via_mem_t* mem)
218 fb.context = mem->context;
220 block = via_mmAllocMem(FBHeap, fb.size, 5, 0);
222 fb.offset = block->ofs;
223 fb.free = (unsigned int)block;
224 if (!add_alloc_set(fb.context, VIDEO, fb.free)) {
225 DRM_DEBUG("adding to allocation set fails\n");
226 via_mmFreeMem((PMemBlock)fb.free);
237 mem->offset = fb.offset;
238 mem->index = fb.free;
240 DRM_DEBUG("alloc fb, size = %d, offset = %d\n", fb.size,
245 int via_agp_alloc(drm_via_mem_t* mem)
254 agp.size = mem->size;
255 agp.context = mem->context;
257 block = via_mmAllocMem(AgpHeap, agp.size, 5, 0);
259 agp.offset = block->ofs;
260 agp.free = (unsigned int)block;
261 if (!add_alloc_set(agp.context, AGP, agp.free)) {
262 DRM_DEBUG("adding to allocation set fails\n");
263 via_mmFreeMem((PMemBlock)agp.free);
273 mem->offset = agp.offset;
274 mem->index = agp.free;
276 DRM_DEBUG("alloc agp, size = %d, offset = %d\n", agp.size,
277 (unsigned int)agp.offset);
281 int via_mem_free( DRM_IOCTL_ARGS )
285 DRM_COPY_FROM_USER_IOCTL(mem, (drm_via_mem_t *)data, sizeof(mem));
290 if (via_fb_free(&mem) == 0)
294 if (via_agp_free(&mem) == 0)
302 int via_fb_free(drm_via_mem_t* mem)
312 fb.free = mem->index;
313 fb.context = mem->context;
321 via_mmFreeMem((PMemBlock)fb.free);
323 if (!del_alloc_set(fb.context, VIDEO, fb.free))
328 DRM_DEBUG("free fb, free = %d\n", fb.free);
333 int via_agp_free(drm_via_mem_t* mem)
339 agp.free = mem->index;
340 agp.context = mem->context;
345 via_mmFreeMem((PMemBlock)agp.free);
347 if (!del_alloc_set(agp.context, AGP, agp.free)) {
351 DRM_DEBUG("free agp, free = %d\n", agp.free);