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 && 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 && global_ppriv[i].context == context) {
63 retval = via_setDel(global_ppriv[i].sets[type], val);
70 /* agp memory management */
71 static memHeap_t *AgpHeap = NULL;
73 int via_agp_init(DRM_IOCTL_ARGS)
77 DRM_COPY_FROM_USER_IOCTL(agp, (drm_via_agp_t *) data, sizeof(agp));
79 AgpHeap = via_mmInit(agp.offset, agp.size);
81 DRM_DEBUG("offset = %lu, size = %lu", (unsigned long)agp.offset, (unsigned long)agp.size);
86 /* fb memory management */
87 static memHeap_t *FBHeap = NULL;
89 int via_fb_init(DRM_IOCTL_ARGS)
93 DRM_COPY_FROM_USER_IOCTL(fb, (drm_via_fb_t *) data, sizeof(fb));
95 FBHeap = via_mmInit(fb.offset, fb.size);
97 DRM_DEBUG("offset = %lu, size = %lu", (unsigned long)fb.offset, (unsigned long)fb.size);
102 int via_init_context(struct drm_device *dev, int context)
106 for (i = 0; i < MAX_CONTEXT; i++)
107 if (global_ppriv[i].used &&
108 (global_ppriv[i].context == context))
111 if (i >= MAX_CONTEXT) {
112 for (i = 0; i < MAX_CONTEXT; i++) {
113 if (!global_ppriv[i].used) {
114 global_ppriv[i].context = context;
115 global_ppriv[i].used = 1;
116 global_ppriv[i].sets[0] = via_setInit();
117 global_ppriv[i].sets[1] = via_setInit();
118 DRM_DEBUG("init allocation set, socket=%d,"
119 " context = %d\n", i, context);
124 if ((i >= MAX_CONTEXT) || (global_ppriv[i].sets[0] == NULL) ||
125 (global_ppriv[i].sets[1] == NULL)) {
133 int via_final_context(struct drm_device *dev, int context)
137 drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
138 drm_via_sarea_t *sAPriv = dev_priv->sarea_priv;
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;
176 * Release futex locks.
179 for (i=0; i < VIA_NR_XVMC_LOCKS; ++i) {
180 lock = XVMCLOCKPTR(sAPriv, i);
181 if ( (_DRM_LOCKING_CONTEXT( *lock ) == context) &&
182 (_DRM_LOCK_IS_HELD( *lock ))) {
183 if ( *lock & _DRM_LOCK_CONT) {
184 DRM_WAKEUP( &(dev_priv->decoder_queue[i]));
190 #if defined(__linux__)
191 /* Linux specific until context tracking code gets ported to BSD */
192 /* Last context, perform cleanup */
193 if (dev->ctx_count == 1 && dev->dev_private) {
195 DRM(irq_uninstall) (dev);
197 via_do_cleanup_map(dev);
204 int via_mem_alloc(DRM_IOCTL_ARGS)
208 DRM_COPY_FROM_USER_IOCTL(mem, (drm_via_mem_t *) data, sizeof(mem));
211 if (via_fb_alloc(&mem) < 0)
213 DRM_COPY_TO_USER_IOCTL((drm_via_mem_t *) data, mem,
217 if (via_agp_alloc(&mem) < 0)
219 DRM_COPY_TO_USER_IOCTL((drm_via_mem_t *) data, mem,
227 int via_fb_alloc(drm_via_mem_t * mem)
237 fb.context = mem->context;
239 block = via_mmAllocMem(FBHeap, fb.size, 5, 0);
241 fb.offset = block->ofs;
242 fb.free = (unsigned int)block;
243 if (!add_alloc_set(fb.context, VIDEO, fb.free)) {
244 DRM_DEBUG("adding to allocation set fails\n");
245 via_mmFreeMem((PMemBlock) fb.free);
255 mem->offset = fb.offset;
256 mem->index = fb.free;
258 DRM_DEBUG("alloc fb, size = %d, offset = %d\n", fb.size,
264 int via_agp_alloc(drm_via_mem_t * mem)
273 agp.size = mem->size;
274 agp.context = mem->context;
276 block = via_mmAllocMem(AgpHeap, agp.size, 5, 0);
278 agp.offset = block->ofs;
279 agp.free = (unsigned int)block;
280 if (!add_alloc_set(agp.context, AGP, agp.free)) {
281 DRM_DEBUG("adding to allocation set fails\n");
282 via_mmFreeMem((PMemBlock) agp.free);
291 mem->offset = agp.offset;
292 mem->index = agp.free;
294 DRM_DEBUG("alloc agp, size = %d, offset = %d\n", agp.size,
295 (unsigned int)agp.offset);
299 int via_mem_free(DRM_IOCTL_ARGS)
303 DRM_COPY_FROM_USER_IOCTL(mem, (drm_via_mem_t *) data, sizeof(mem));
308 if (via_fb_free(&mem) == 0)
312 if (via_agp_free(&mem) == 0)
320 int via_fb_free(drm_via_mem_t * mem)
329 fb.free = mem->index;
330 fb.context = mem->context;
337 via_mmFreeMem((PMemBlock) fb.free);
339 if (!del_alloc_set(fb.context, VIDEO, fb.free)) {
343 DRM_DEBUG("free fb, free = %d\n", fb.free);
348 int via_agp_free(drm_via_mem_t * mem)
354 agp.free = mem->index;
355 agp.context = mem->context;
360 via_mmFreeMem((PMemBlock) agp.free);
362 if (!del_alloc_set(agp.context, AGP, agp.free)) {
366 DRM_DEBUG("free agp, free = %d\n", agp.free);
371 EXPORT_SYMBOL(via_fb_alloc);
372 EXPORT_SYMBOL(via_fb_free);
374 void DRM(driver_register_fns) (drm_device_t * dev) {
375 dev->driver_features =
376 DRIVER_USE_AGP | DRIVER_USE_MTRR | DRIVER_HAVE_IRQ |
377 DRIVER_IRQ_SHARED | DRIVER_IRQ_VBL;
378 dev->fn_tbl.context_ctor = via_init_context;
379 dev->fn_tbl.context_dtor = via_final_context;
380 dev->fn_tbl.vblank_wait = via_driver_vblank_wait;
381 dev->fn_tbl.irq_preinstall = via_driver_irq_preinstall;
382 dev->fn_tbl.irq_postinstall = via_driver_irq_postinstall;
383 dev->fn_tbl.irq_uninstall = via_driver_irq_uninstall;
384 dev->fn_tbl.irq_handler = via_driver_irq_handler;
385 dev->fn_tbl.dma_quiescent = via_driver_dma_quiescent;