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 long 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 long 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(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
);
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
);
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
)
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
))
143 if (i
< MAX_CONTEXT
) {
148 DRM_DEBUG("find socket %d, context = %d\n", i
, context
);
151 set
= global_ppriv
[i
].sets
[0];
152 retval
= via_setFirst(set
, &item
);
154 DRM_DEBUG("free video memory 0x%lx\n", item
);
155 via_mmFreeMem((PMemBlock
) item
);
156 retval
= via_setNext(set
, &item
);
161 set
= global_ppriv
[i
].sets
[1];
162 retval
= via_setFirst(set
, &item
);
164 DRM_DEBUG("free agp memory 0x%lx\n", item
);
165 via_mmFreeMem((PMemBlock
) item
);
166 retval
= via_setNext(set
, &item
);
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");
179 drm_irq_uninstall(dev
);
181 via_cleanup_futex(dev_priv
);
182 via_do_cleanup_map(dev
);
189 int via_mem_alloc(struct drm_device
*dev
, void *data
, struct drm_file
*file_priv
)
191 drm_via_mem_t
*mem
= data
;
195 if (via_fb_alloc(mem
) < 0)
199 if (via_agp_alloc(mem
) < 0)
207 static int via_fb_alloc(drm_via_mem_t
* mem
)
217 fb
.context
= mem
->context
;
219 block
= via_mmAllocMem(FBHeap
, fb
.size
, 5, 0);
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
);
235 mem
->offset
= fb
.offset
;
236 mem
->index
= fb
.free
;
238 DRM_DEBUG("alloc fb, size = %d, offset = %d\n", fb
.size
,
244 static int via_agp_alloc(drm_via_mem_t
* mem
)
253 agp
.size
= mem
->size
;
254 agp
.context
= mem
->context
;
256 block
= via_mmAllocMem(AgpHeap
, agp
.size
, 5, 0);
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
);
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
);
279 int via_mem_free(struct drm_device
*dev
, void *data
, struct drm_file
*file_priv
)
281 drm_via_mem_t
*mem
= data
;
286 if (via_fb_free(mem
) == 0)
290 if (via_agp_free(mem
) == 0)
298 static int via_fb_free(drm_via_mem_t
* mem
)
307 fb
.free
= mem
->index
;
308 fb
.context
= mem
->context
;
315 via_mmFreeMem((PMemBlock
) fb
.free
);
317 if (!del_alloc_set(fb
.context
, VIA_MEM_VIDEO
, fb
.free
)) {
321 DRM_DEBUG("free fb, free = %ld\n", fb
.free
);
326 static int via_agp_free(drm_via_mem_t
* mem
)
332 agp
.free
= mem
->index
;
333 agp
.context
= mem
->context
;
338 via_mmFreeMem((PMemBlock
) agp
.free
);
340 if (!del_alloc_set(agp
.context
, VIA_MEM_AGP
, agp
.free
)) {
344 DRM_DEBUG("free agp, free = %ld\n", agp
.free
);