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
35 set_t *sets[2]; /* 0 for frame buffer, 1 for AGP , 2 for System */
38 static via_context_t global_ppriv[MAX_CONTEXT];
40 static int via_agp_alloc(drm_via_mem_t * mem);
41 static int via_agp_free(drm_via_mem_t * mem);
42 static int via_fb_alloc(drm_via_mem_t * mem);
43 static int via_fb_free(drm_via_mem_t * mem);
45 static int add_alloc_set(int context, int type, unsigned int val)
49 for (i = 0; i < MAX_CONTEXT; i++) {
50 if (global_ppriv[i].used && 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 && 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 = %lu, size = %lu", (unsigned long)agp.offset, (unsigned long)agp.size);
88 /* fb memory management */
89 static memHeap_t *FBHeap = NULL;
91 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 = %lu, size = %lu", (unsigned long)fb.offset, (unsigned long)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)
139 drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
140 drm_via_sarea_t *sAPriv = dev_priv->sarea_priv;
142 for (i = 0; i < MAX_CONTEXT; i++)
143 if (global_ppriv[i].used &&
144 (global_ppriv[i].context == context))
147 if (i < MAX_CONTEXT) {
152 DRM_DEBUG("find socket %d, context = %d\n", i, context);
155 set = global_ppriv[i].sets[0];
156 retval = via_setFirst(set, &item);
158 DRM_DEBUG("free video memory 0x%x\n", item);
159 via_mmFreeMem((PMemBlock) item);
160 retval = via_setNext(set, &item);
165 set = global_ppriv[i].sets[1];
166 retval = via_setFirst(set, &item);
168 DRM_DEBUG("free agp memory 0x%x\n", item);
169 via_mmFreeMem((PMemBlock) item);
170 retval = via_setNext(set, &item);
174 global_ppriv[i].used = 0;
178 * Release futex locks.
181 for (i=0; i < VIA_NR_XVMC_LOCKS; ++i) {
182 lock = (int *) XVMCLOCKPTR(sAPriv, i);
183 if ( (_DRM_LOCKING_CONTEXT( *lock ) == context)) {
184 if (_DRM_LOCK_IS_HELD( *lock ) && (*lock & _DRM_LOCK_CONT)) {
185 DRM_WAKEUP( &(dev_priv->decoder_queue[i]));
191 #if defined(__linux__)
192 /* Linux specific until context tracking code gets ported to BSD */
193 /* Last context, perform cleanup */
194 if (dev->ctx_count == 1 && dev->dev_private) {
195 DRM_DEBUG("Last Context\n");
197 drm_irq_uninstall(dev);
199 via_do_cleanup_map(dev);
206 int via_mem_alloc(DRM_IOCTL_ARGS)
210 DRM_COPY_FROM_USER_IOCTL(mem, (drm_via_mem_t *) data, sizeof(mem));
213 if (via_fb_alloc(&mem) < 0)
215 DRM_COPY_TO_USER_IOCTL((drm_via_mem_t *) data, mem,
219 if (via_agp_alloc(&mem) < 0)
221 DRM_COPY_TO_USER_IOCTL((drm_via_mem_t *) data, mem,
229 static int via_fb_alloc(drm_via_mem_t * mem)
239 fb.context = mem->context;
241 block = via_mmAllocMem(FBHeap, fb.size, 5, 0);
243 fb.offset = block->ofs;
244 fb.free = (unsigned int)block;
245 if (!add_alloc_set(fb.context, VIDEO, fb.free)) {
246 DRM_DEBUG("adding to allocation set fails\n");
247 via_mmFreeMem((PMemBlock) fb.free);
257 mem->offset = fb.offset;
258 mem->index = fb.free;
260 DRM_DEBUG("alloc fb, size = %d, offset = %d\n", fb.size,
266 static int via_agp_alloc(drm_via_mem_t * mem)
275 agp.size = mem->size;
276 agp.context = mem->context;
278 block = via_mmAllocMem(AgpHeap, agp.size, 5, 0);
280 agp.offset = block->ofs;
281 agp.free = (unsigned int)block;
282 if (!add_alloc_set(agp.context, AGP, agp.free)) {
283 DRM_DEBUG("adding to allocation set fails\n");
284 via_mmFreeMem((PMemBlock) agp.free);
293 mem->offset = agp.offset;
294 mem->index = agp.free;
296 DRM_DEBUG("alloc agp, size = %d, offset = %d\n", agp.size,
297 (unsigned int)agp.offset);
301 int via_mem_free(DRM_IOCTL_ARGS)
305 DRM_COPY_FROM_USER_IOCTL(mem, (drm_via_mem_t *) data, sizeof(mem));
310 if (via_fb_free(&mem) == 0)
314 if (via_agp_free(&mem) == 0)
322 static int via_fb_free(drm_via_mem_t * mem)
331 fb.free = mem->index;
332 fb.context = mem->context;
339 via_mmFreeMem((PMemBlock) fb.free);
341 if (!del_alloc_set(fb.context, VIDEO, fb.free)) {
345 DRM_DEBUG("free fb, free = %d\n", fb.free);
350 static int via_agp_free(drm_via_mem_t * mem)
356 agp.free = mem->index;
357 agp.context = mem->context;
362 via_mmFreeMem((PMemBlock) agp.free);
364 if (!del_alloc_set(agp.context, AGP, agp.free)) {
368 DRM_DEBUG("free agp, free = %d\n", agp.free);