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.
31 #define MAX_CONTEXT 100
33 unsigned int VIA_DEBUG = 1;
38 set_t *sets[2]; /* 0 for frame buffer, 1 for AGP , 2 for System*/
41 static via_context_t global_ppriv[MAX_CONTEXT];
43 static int add_alloc_set(int context, int type, unsigned int val)
47 for (i = 0; i < MAX_CONTEXT; i++) {
48 if (global_ppriv[i].used &&
49 global_ppriv[i].context == context) {
50 retval = via_setAdd(global_ppriv[i].sets[type], val);
58 static int del_alloc_set(int context, int type, unsigned int val)
62 for (i = 0; i < MAX_CONTEXT; i++)
63 if (global_ppriv[i].used &&
64 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 *)data, sizeof(agp));
81 AgpHeap = via_mmInit(agp.offset, agp.size);
83 DRM_DEBUG("offset = %u, size = %u", agp.offset, agp.size);
88 /* fb memory management */
89 static memHeap_t *FBHeap = NULL;
91 int via_fb_init( DRM_IOCTL_ARGS )
96 DRM_COPY_FROM_USER_IOCTL(fb, (drm_via_fb_t *)data, sizeof(fb));
98 FBHeap = via_mmInit(fb.offset, fb.size);
100 DRM_DEBUG("offset = %u, size = %u", fb.offset, fb.size);
105 int via_init_context(struct drm_device *dev, int context)
109 for (i = 0; i < MAX_CONTEXT ; i++)
110 if (global_ppriv[i].used &&
111 (global_ppriv[i].context == context))
114 if (i >= MAX_CONTEXT) {
115 for (i = 0; i < MAX_CONTEXT ; i++) {
116 if (!global_ppriv[i].used) {
117 global_ppriv[i].context = context;
118 global_ppriv[i].used = 1;
119 global_ppriv[i].sets[0] = via_setInit();
120 global_ppriv[i].sets[1] = via_setInit();
121 DRM_DEBUG("init allocation set, socket=%d,"
122 " context = %d\n", i, context);
127 if ((i >= MAX_CONTEXT) || (global_ppriv[i].sets[0] == NULL) ||
128 (global_ppriv[i].sets[1] == NULL)) {
136 int via_final_context(struct drm_device *dev, int context)
139 for (i=0; i<MAX_CONTEXT; i++)
140 if (global_ppriv[i].used &&
141 (global_ppriv[i].context == context))
144 if (i < MAX_CONTEXT) {
149 DRM_DEBUG("find socket %d, context = %d\n", i, context);
152 set = global_ppriv[i].sets[0];
153 retval = via_setFirst(set, &item);
155 DRM_DEBUG("free video memory 0x%x\n", item);
156 via_mmFreeMem((PMemBlock)item);
157 retval = via_setNext(set, &item);
162 set = global_ppriv[i].sets[1];
163 retval = via_setFirst(set, &item);
165 DRM_DEBUG("free agp memory 0x%x\n", item);
166 via_mmFreeMem((PMemBlock)item);
167 retval = via_setNext(set, &item);
171 global_ppriv[i].used = 0;
174 #if defined(__linux__)
175 /* Linux specific until context tracking code gets ported to BSD */
176 /* Last context, perform cleanup */
177 if (dev->ctx_count == 1 && dev->dev_private) {
178 if (dev->irq) DRM(irq_uninstall)(dev);
180 via_do_cleanup_map(dev);
186 int via_mem_alloc( DRM_IOCTL_ARGS)
190 DRM_COPY_FROM_USER_IOCTL(mem, (drm_via_mem_t *)data, sizeof(mem));
193 if (via_fb_alloc(&mem) < 0)
195 DRM_COPY_TO_USER_IOCTL((drm_via_mem_t *)data, mem,
199 if (via_agp_alloc(&mem) < 0)
201 DRM_COPY_TO_USER_IOCTL((drm_via_mem_t *)data, mem,
209 int via_fb_alloc(drm_via_mem_t* mem)
219 fb.context = mem->context;
221 block = via_mmAllocMem(FBHeap, fb.size, 5, 0);
223 fb.offset = block->ofs;
224 fb.free = (unsigned int)block;
225 if (!add_alloc_set(fb.context, VIDEO, fb.free)) {
226 DRM_DEBUG("adding to allocation set fails\n");
227 via_mmFreeMem((PMemBlock)fb.free);
238 mem->offset = fb.offset;
239 mem->index = fb.free;
241 DRM_DEBUG("alloc fb, size = %d, offset = %d\n", fb.size,
246 int via_agp_alloc(drm_via_mem_t* mem)
255 agp.size = mem->size;
256 agp.context = mem->context;
258 block = via_mmAllocMem(AgpHeap, agp.size, 5, 0);
260 agp.offset = block->ofs;
261 agp.free = (unsigned int)block;
262 if (!add_alloc_set(agp.context, AGP, agp.free)) {
263 DRM_DEBUG("adding to allocation set fails\n");
264 via_mmFreeMem((PMemBlock)agp.free);
274 mem->offset = agp.offset;
275 mem->index = agp.free;
277 DRM_DEBUG("alloc agp, size = %d, offset = %d\n", agp.size,
278 (unsigned int)agp.offset);
282 int via_mem_free( DRM_IOCTL_ARGS )
286 DRM_COPY_FROM_USER_IOCTL(mem, (drm_via_mem_t *)data, sizeof(mem));
291 if (via_fb_free(&mem) == 0)
295 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);
332 int via_agp_free(drm_via_mem_t* mem)
338 agp.free = mem->index;
339 agp.context = mem->context;
344 via_mmFreeMem((PMemBlock)agp.free);
346 if (!del_alloc_set(agp.context, AGP, agp.free)) {
350 DRM_DEBUG("free agp, free = %d\n", agp.free);
355 EXPORT_SYMBOL(via_fb_alloc);
356 EXPORT_SYMBOL(via_fb_free);
358 void DRM(driver_register_fns)(drm_device_t *dev)
360 dev->driver_features = DRIVER_USE_AGP | DRIVER_USE_MTRR | DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED | DRIVER_IRQ_VBL;
361 dev->fn_tbl.context_ctor = via_init_context;
362 dev->fn_tbl.context_dtor = via_final_context;
363 dev->fn_tbl.vblank_wait = via_driver_vblank_wait;
364 dev->fn_tbl.irq_preinstall = via_driver_irq_preinstall;
365 dev->fn_tbl.irq_postinstall = via_driver_irq_postinstall;
366 dev->fn_tbl.irq_uninstall = via_driver_irq_uninstall;
367 dev->fn_tbl.irq_handler = via_driver_irq_handler;