No empty .Rs/.Re
[netbsd-mini2440.git] / sys / external / bsd / drm / dist / shared-core / via_mm.c
blobadd888df246ccdb8925c2b96509ec2c261c79872
1 /*
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
14 * of the Software.
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 #include "drmP.h"
25 #include "via_drm.h"
26 #include "via_drv.h"
27 #include "via_ds.h"
28 #include "via_mm.h"
30 #define MAX_CONTEXT 100
32 typedef struct {
33 int used;
34 int context;
35 set_t *sets[2]; /* 0 for frame buffer, 1 for AGP , 2 for System */
36 } via_context_t;
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 long val)
47 int i, retval = 0;
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);
52 break;
56 return retval;
59 static int del_alloc_set(int context, int type, unsigned long val)
61 int i, retval = 0;
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);
66 break;
69 return retval;
72 /* agp memory management */
73 static memHeap_t *AgpHeap = NULL;
75 int via_agp_init(struct drm_device *dev, void *data, struct drm_file *file_priv)
77 drm_via_agp_t *agp = data;
79 AgpHeap = via_mmInit(agp->offset, agp->size);
81 DRM_DEBUG("offset = %lu, size = %lu", (unsigned long)agp->offset,
82 (unsigned long)agp->size);
84 return 0;
87 /* fb memory management */
88 static memHeap_t *FBHeap = NULL;
90 int via_fb_init(struct drm_device *dev, void *data, struct drm_file *file_priv)
92 drm_via_fb_t *fb = data;
94 FBHeap = via_mmInit(fb->offset, fb->size);
96 DRM_DEBUG("offset = %lu, size = %lu", (unsigned long)fb->offset,
97 (unsigned long)fb->size);
99 return 0;
102 int via_init_context(struct drm_device *dev, int context)
104 int i;
106 for (i = 0; i < MAX_CONTEXT; i++)
107 if (global_ppriv[i].used &&
108 (global_ppriv[i].context == context))
109 break;
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);
120 break;
124 if ((i >= MAX_CONTEXT) || (global_ppriv[i].sets[0] == NULL) ||
125 (global_ppriv[i].sets[1] == NULL)) {
126 return 0;
130 return 1;
133 int via_final_context(struct drm_device *dev, int context)
135 int i;
136 drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
138 for (i = 0; i < MAX_CONTEXT; i++)
139 if (global_ppriv[i].used &&
140 (global_ppriv[i].context == context))
141 break;
143 if (i < MAX_CONTEXT) {
144 set_t *set;
145 ITEM_TYPE item;
146 int retval;
148 DRM_DEBUG("find socket %d, context = %d\n", i, context);
150 /* Video Memory */
151 set = global_ppriv[i].sets[0];
152 retval = via_setFirst(set, &item);
153 while (retval) {
154 DRM_DEBUG("free video memory 0x%lx\n", item);
155 via_mmFreeMem((PMemBlock) item);
156 retval = via_setNext(set, &item);
158 via_setDestroy(set);
160 /* AGP Memory */
161 set = global_ppriv[i].sets[1];
162 retval = via_setFirst(set, &item);
163 while (retval) {
164 DRM_DEBUG("free agp memory 0x%lx\n", item);
165 via_mmFreeMem((PMemBlock) item);
166 retval = via_setNext(set, &item);
168 via_setDestroy(set);
169 global_ppriv[i].used = 0;
171 via_release_futex(dev_priv, context);
173 #if defined(__linux__)
174 /* Linux specific until context tracking code gets ported to BSD */
175 /* Last context, perform cleanup */
176 if (dev->ctx_count == 1 && dev->dev_private) {
177 DRM_DEBUG("Last Context\n");
178 if (dev->irq)
179 drm_irq_uninstall(dev);
181 via_cleanup_futex(dev_priv);
182 via_do_cleanup_map(dev);
184 #endif
186 return 1;
189 int via_mem_alloc(struct drm_device *dev, void *data, struct drm_file *file_priv)
191 drm_via_mem_t *mem = data;
193 switch (mem->type) {
194 case VIA_MEM_VIDEO:
195 if (via_fb_alloc(mem) < 0)
196 return -EFAULT;
197 return 0;
198 case VIA_MEM_AGP:
199 if (via_agp_alloc(mem) < 0)
200 return -EFAULT;
201 return 0;
204 return -EFAULT;
207 static int via_fb_alloc(drm_via_mem_t * mem)
209 drm_via_mm_t fb;
210 PMemBlock block;
211 int retval = 0;
213 if (!FBHeap)
214 return -1;
216 fb.size = mem->size;
217 fb.context = mem->context;
219 block = via_mmAllocMem(FBHeap, fb.size, 5, 0);
220 if (block) {
221 fb.offset = block->ofs;
222 fb.free = (unsigned long)block;
223 if (!add_alloc_set(fb.context, VIA_MEM_VIDEO, fb.free)) {
224 DRM_DEBUG("adding to allocation set fails\n");
225 via_mmFreeMem((PMemBlock) fb.free);
226 retval = -1;
228 } else {
229 fb.offset = 0;
230 fb.size = 0;
231 fb.free = 0;
232 retval = -1;
235 mem->offset = fb.offset;
236 mem->index = fb.free;
238 DRM_DEBUG("alloc fb, size = %d, offset = %d\n", fb.size,
239 (int)fb.offset);
241 return retval;
244 static int via_agp_alloc(drm_via_mem_t * mem)
246 drm_via_mm_t agp;
247 PMemBlock block;
248 int retval = 0;
250 if (!AgpHeap)
251 return -1;
253 agp.size = mem->size;
254 agp.context = mem->context;
256 block = via_mmAllocMem(AgpHeap, agp.size, 5, 0);
257 if (block) {
258 agp.offset = block->ofs;
259 agp.free = (unsigned long)block;
260 if (!add_alloc_set(agp.context, VIA_MEM_AGP, agp.free)) {
261 DRM_DEBUG("adding to allocation set fails\n");
262 via_mmFreeMem((PMemBlock) agp.free);
263 retval = -1;
265 } else {
266 agp.offset = 0;
267 agp.size = 0;
268 agp.free = 0;
271 mem->offset = agp.offset;
272 mem->index = agp.free;
274 DRM_DEBUG("alloc agp, size = %d, offset = %d\n", agp.size,
275 (unsigned int)agp.offset);
276 return retval;
279 int via_mem_free(struct drm_device *dev, void *data, struct drm_file *file_priv)
281 drm_via_mem_t *mem = data;
283 switch (mem->type) {
285 case VIA_MEM_VIDEO:
286 if (via_fb_free(mem) == 0)
287 return 0;
288 break;
289 case VIA_MEM_AGP:
290 if (via_agp_free(mem) == 0)
291 return 0;
292 break;
295 return -EFAULT;
298 static int via_fb_free(drm_via_mem_t * mem)
300 drm_via_mm_t fb;
301 int retval = 0;
303 if (!FBHeap) {
304 return -1;
307 fb.free = mem->index;
308 fb.context = mem->context;
310 if (!fb.free) {
311 return -1;
315 via_mmFreeMem((PMemBlock) fb.free);
317 if (!del_alloc_set(fb.context, VIA_MEM_VIDEO, fb.free)) {
318 retval = -1;
321 DRM_DEBUG("free fb, free = %ld\n", fb.free);
323 return retval;
326 static int via_agp_free(drm_via_mem_t * mem)
328 drm_via_mm_t agp;
330 int retval = 0;
332 agp.free = mem->index;
333 agp.context = mem->context;
335 if (!agp.free)
336 return -1;
338 via_mmFreeMem((PMemBlock) agp.free);
340 if (!del_alloc_set(agp.context, VIA_MEM_AGP, agp.free)) {
341 retval = -1;
344 DRM_DEBUG("free agp, free = %ld\n", agp.free);
346 return retval;