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.
24 #define __NO_VERSION__
32 #define MAX_CONTEXT 100
34 unsigned int VIA_DEBUG = 1;
39 set_t *sets[2]; /* 0 for frame buffer, 1 for AGP , 2 for System*/
42 static via_context_t global_ppriv[MAX_CONTEXT];
44 static int add_alloc_set(int context, int type, unsigned int val)
48 for (i = 0; i < MAX_CONTEXT; i++) {
49 if (global_ppriv[i].used &&
50 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 &&
65 global_ppriv[i].context == context) {
66 retval = via_setDel(global_ppriv[i].sets[type], val);
73 /* agp memory management */
74 static memHeap_t *AgpHeap = NULL;
76 int via_agp_init( DRM_IOCTL_ARGS )
80 DRM_COPY_FROM_USER_IOCTL(agp, (drm_via_agp_t *)data, sizeof(agp));
82 AgpHeap = via_mmInit(agp.offset, agp.size);
84 DRM_DEBUG("offset = %u, size = %u", agp.offset, agp.size);
89 /* fb memory management */
90 static memHeap_t *FBHeap = NULL;
92 int via_fb_init( DRM_IOCTL_ARGS )
97 DRM_COPY_FROM_USER_IOCTL(fb, (drm_via_fb_t *)data, sizeof(fb));
99 FBHeap = via_mmInit(fb.offset, fb.size);
101 DRM_DEBUG("offset = %u, size = %u", fb.offset, fb.size);
106 int via_init_context(struct drm_device *dev, int context)
110 for (i = 0; i < MAX_CONTEXT ; i++)
111 if (global_ppriv[i].used &&
112 (global_ppriv[i].context == context))
115 if (i >= MAX_CONTEXT) {
116 for (i = 0; i < MAX_CONTEXT ; i++) {
117 if (!global_ppriv[i].used) {
118 global_ppriv[i].context = context;
119 global_ppriv[i].used = 1;
120 global_ppriv[i].sets[0] = via_setInit();
121 global_ppriv[i].sets[1] = via_setInit();
122 DRM_DEBUG("init allocation set, socket=%d,"
123 " context = %d\n", i, context);
128 if ((i >= MAX_CONTEXT) || (global_ppriv[i].sets[0] == NULL) ||
129 (global_ppriv[i].sets[1] == NULL)) {
137 int via_final_context(struct drm_device *dev, int context)
140 for (i=0; i<MAX_CONTEXT; i++)
141 if (global_ppriv[i].used &&
142 (global_ppriv[i].context == context))
145 if (i < MAX_CONTEXT) {
150 DRM_DEBUG("find socket %d, context = %d\n", i, context);
153 set = global_ppriv[i].sets[0];
154 retval = via_setFirst(set, &item);
156 DRM_DEBUG("free video memory 0x%x\n", item);
157 via_mmFreeMem((PMemBlock)item);
158 retval = via_setNext(set, &item);
163 set = global_ppriv[i].sets[1];
164 retval = via_setFirst(set, &item);
166 DRM_DEBUG("free agp memory 0x%x\n", item);
167 via_mmFreeMem((PMemBlock)item);
168 retval = via_setNext(set, &item);
172 global_ppriv[i].used = 0;
175 #if defined(__linux__)
176 /* Linux specific until context tracking code gets ported to BSD */
177 /* Last context, perform cleanup */
178 if (dev->ctx_count == 1 && dev->dev_private) {
179 if (dev->irq) DRM(irq_uninstall)(dev);
181 via_do_cleanup_map(dev);
187 int via_mem_alloc( DRM_IOCTL_ARGS)
191 DRM_COPY_FROM_USER_IOCTL(mem, (drm_via_mem_t *)data, sizeof(mem));
194 if (via_fb_alloc(&mem) < 0)
196 DRM_COPY_TO_USER_IOCTL((drm_via_mem_t *)data, mem,
200 if (via_agp_alloc(&mem) < 0)
202 DRM_COPY_TO_USER_IOCTL((drm_via_mem_t *)data, mem,
210 int via_fb_alloc(drm_via_mem_t* mem)
220 fb.context = mem->context;
222 block = via_mmAllocMem(FBHeap, fb.size, 5, 0);
224 fb.offset = block->ofs;
225 fb.free = (unsigned int)block;
226 if (!add_alloc_set(fb.context, VIDEO, fb.free)) {
227 DRM_DEBUG("adding to allocation set fails\n");
228 via_mmFreeMem((PMemBlock)fb.free);
239 mem->offset = fb.offset;
240 mem->index = fb.free;
242 DRM_DEBUG("alloc fb, size = %d, offset = %d\n", fb.size,
247 int via_agp_alloc(drm_via_mem_t* mem)
256 agp.size = mem->size;
257 agp.context = mem->context;
259 block = via_mmAllocMem(AgpHeap, agp.size, 5, 0);
261 agp.offset = block->ofs;
262 agp.free = (unsigned int)block;
263 if (!add_alloc_set(agp.context, AGP, agp.free)) {
264 DRM_DEBUG("adding to allocation set fails\n");
265 via_mmFreeMem((PMemBlock)agp.free);
275 mem->offset = agp.offset;
276 mem->index = agp.free;
278 DRM_DEBUG("alloc agp, size = %d, offset = %d\n", agp.size,
279 (unsigned int)agp.offset);
283 int via_mem_free( DRM_IOCTL_ARGS )
287 DRM_COPY_FROM_USER_IOCTL(mem, (drm_via_mem_t *)data, sizeof(mem));
292 if (via_fb_free(&mem) == 0)
296 if (via_agp_free(&mem) == 0)
303 int via_fb_free(drm_via_mem_t* mem)
313 fb.free = mem->index;
314 fb.context = mem->context;
322 via_mmFreeMem((PMemBlock)fb.free);
324 if (!del_alloc_set(fb.context, VIDEO, fb.free))
329 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);
356 EXPORT_SYMBOL(via_fb_alloc);
357 EXPORT_SYMBOL(via_fb_free);
359 void DRM(driver_register_fns)(drm_device_t *dev)
361 dev->fn_tbl.context_ctor = via_init_context;
362 dev->fn_tbl.context_dtor = via_final_context;