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 && global_ppriv[i].context == context) {
48 retval = via_setAdd(global_ppriv[i].sets[type], val);
56 static int del_alloc_set(int context, int type, unsigned int val)
60 for (i = 0; i < MAX_CONTEXT; i++)
61 if (global_ppriv[i].used && global_ppriv[i].context == context) {
62 retval = via_setDel(global_ppriv[i].sets[type], val);
69 /* agp memory management */
70 static memHeap_t *AgpHeap = NULL;
72 int via_agp_init(DRM_IOCTL_ARGS)
76 DRM_COPY_FROM_USER_IOCTL(agp, (drm_via_agp_t *) data, sizeof(agp));
78 AgpHeap = via_mmInit(agp.offset, agp.size);
80 DRM_DEBUG("offset = %lu, size = %lu", (unsigned long)agp.offset, (unsigned long)agp.size);
85 /* fb memory management */
86 static memHeap_t *FBHeap = NULL;
88 int via_fb_init(DRM_IOCTL_ARGS)
92 DRM_COPY_FROM_USER_IOCTL(fb, (drm_via_fb_t *) data, sizeof(fb));
94 FBHeap = via_mmInit(fb.offset, fb.size);
96 DRM_DEBUG("offset = %lu, size = %lu", (unsigned long)fb.offset, (unsigned long)fb.size);
101 int via_init_context(struct drm_device *dev, int context)
105 for (i = 0; i < MAX_CONTEXT; i++)
106 if (global_ppriv[i].used &&
107 (global_ppriv[i].context == context))
110 if (i >= MAX_CONTEXT) {
111 for (i = 0; i < MAX_CONTEXT; i++) {
112 if (!global_ppriv[i].used) {
113 global_ppriv[i].context = context;
114 global_ppriv[i].used = 1;
115 global_ppriv[i].sets[0] = via_setInit();
116 global_ppriv[i].sets[1] = via_setInit();
117 DRM_DEBUG("init allocation set, socket=%d,"
118 " context = %d\n", i, context);
123 if ((i >= MAX_CONTEXT) || (global_ppriv[i].sets[0] == NULL) ||
124 (global_ppriv[i].sets[1] == NULL)) {
132 int via_final_context(struct drm_device *dev, int context)
136 drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
137 drm_via_sarea_t *sAPriv = dev_priv->sarea_priv;
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;
175 * Release futex locks.
178 for (i=0; i < VIA_NR_XVMC_LOCKS; ++i) {
179 lock = XVMCLOCKPTR(sAPriv, i);
180 if ( (_DRM_LOCKING_CONTEXT( *lock ) == i) &&
181 (_DRM_LOCK_IS_HELD( *lock ))) {
182 if ( *lock & _DRM_LOCK_CONT) {
183 DRM_WAKEUP( &(dev_priv->decoder_queue[i]));
185 *lock &= ~( _DRM_LOCK_HELD | _DRM_LOCK_CONT );
189 #if defined(__linux__)
190 /* Linux specific until context tracking code gets ported to BSD */
191 /* Last context, perform cleanup */
192 if (dev->ctx_count == 1 && dev->dev_private) {
193 DRM_DEBUG("Last Context\n");
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);