1 /* sis_mm.c -- Private header for Direct Rendering Manager -*- linux-c -*-
2 * Created: Mon Jan 4 10:05:05 1999 by sclin@sis.com.tw
4 * Copyright 2000 Silicon Integrated Systems Corp, Inc., HsinChu, Taiwan.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
27 * Sung-Ching Lin <sclin@sis.com.tw>
36 #if defined(__linux__) && defined(CONFIG_FB_SIS)
37 #include <video/sisfb.h>
40 #define MAX_CONTEXT 100
47 set_t *sets[2]; /* 0 for video, 1 for AGP */
50 static sis_context_t global_ppriv[MAX_CONTEXT];
53 static int add_alloc_set(int context, int type, unsigned int val)
57 for (i = 0; i < MAX_CONTEXT; i++) {
58 if (global_ppriv[i].used && global_ppriv[i].context == context)
60 retval = setAdd(global_ppriv[i].sets[type], val);
67 static int del_alloc_set(int context, int type, unsigned int val)
71 for (i = 0; i < MAX_CONTEXT; i++) {
72 if (global_ppriv[i].used && global_ppriv[i].context == context)
74 retval = setDel(global_ppriv[i].sets[type], val);
81 /* fb management via fb device */
82 #if defined(__linux__) && defined(CONFIG_FB_SIS)
84 int sis_fb_init( DRM_IOCTL_ARGS )
89 int sis_fb_alloc( DRM_IOCTL_ARGS )
92 struct sis_memreq req;
95 DRM_COPY_FROM_USER_IOCTL(fb, (drm_sis_mem_t *)data, sizeof(fb));
101 fb.offset = req.offset;
102 fb.free = req.offset;
103 if (!add_alloc_set(fb.context, VIDEO_TYPE, fb.free)) {
104 DRM_DEBUG("adding to allocation set fails\n");
105 sis_free(req.offset);
106 retval = DRM_ERR(EINVAL);
114 DRM_COPY_TO_USER_IOCTL((drm_sis_mem_t *)data, fb, sizeof(fb));
116 DRM_DEBUG("alloc fb, size = %d, offset = %ld\n", fb.size, req.offset);
121 int sis_fb_free( DRM_IOCTL_ARGS )
126 DRM_COPY_FROM_USER_IOCTL(fb, (drm_sis_mem_t *)data, sizeof(fb));
129 return DRM_ERR(EINVAL);
131 if (!del_alloc_set(fb.context, VIDEO_TYPE, fb.free))
132 retval = DRM_ERR(EINVAL);
135 DRM_DEBUG("free fb, offset = %lu\n", fb.free);
142 /* Called by the X Server to initialize the FB heap. Allocations will fail
143 * unless this is called. Offset is the beginning of the heap from the
144 * framebuffer offset (MaxXFBMem in XFree86).
146 * Memory layout according to Thomas Winischofer:
147 * |------------------|DDDDDDDDDDDDDDDDDDDDDDDDDDDDD|HHHH|CCCCCCCCCCC|
149 * X driver/sisfb HW- Command-
150 * framebuffer memory DRI heap Cursor queue
152 int sis_fb_init( DRM_IOCTL_ARGS )
155 drm_sis_private_t *dev_priv = dev->dev_private;
158 DRM_COPY_FROM_USER_IOCTL(fb, (drm_sis_fb_t *)data, sizeof(fb));
160 if (dev_priv == NULL) {
161 dev->dev_private = DRM(calloc)(1, sizeof(drm_sis_private_t),
163 dev_priv = dev->dev_private;
164 if (dev_priv == NULL)
168 if (dev_priv->FBHeap != NULL)
169 return DRM_ERR(EINVAL);
171 dev_priv->FBHeap = mmInit(fb.offset, fb.size);
173 DRM_DEBUG("offset = %u, size = %u", fb.offset, fb.size);
178 int sis_fb_alloc( DRM_IOCTL_ARGS )
181 drm_sis_private_t *dev_priv = dev->dev_private;
186 if (dev_priv == NULL || dev_priv->FBHeap == NULL)
187 return DRM_ERR(EINVAL);
189 DRM_COPY_FROM_USER_IOCTL(fb, (drm_sis_mem_t *)data, sizeof(fb));
191 block = mmAllocMem(dev_priv->FBHeap, fb.size, 0, 0);
194 fb.offset = block->ofs;
195 fb.free = (unsigned long)block;
196 if (!add_alloc_set(fb.context, VIDEO_TYPE, fb.free)) {
197 DRM_DEBUG("adding to allocation set fails\n");
198 mmFreeMem((PMemBlock)fb.free);
199 retval = DRM_ERR(EINVAL);
207 DRM_COPY_TO_USER_IOCTL((drm_sis_mem_t *)data, fb, sizeof(fb));
209 DRM_DEBUG("alloc fb, size = %d, offset = %d\n", fb.size, fb.offset);
214 int sis_fb_free( DRM_IOCTL_ARGS )
217 drm_sis_private_t *dev_priv = dev->dev_private;
220 if (dev_priv == NULL || dev_priv->FBHeap == NULL)
221 return DRM_ERR(EINVAL);
223 DRM_COPY_FROM_USER_IOCTL(fb, (drm_sis_mem_t *)data, sizeof(fb));
225 if (!mmBlockInHeap(dev_priv->FBHeap, (PMemBlock)fb.free))
226 return DRM_ERR(EINVAL);
228 if (!del_alloc_set(fb.context, VIDEO_TYPE, fb.free))
229 return DRM_ERR(EINVAL);
230 mmFreeMem((PMemBlock)fb.free);
232 DRM_DEBUG("free fb, free = 0x%lx\n", fb.free);
239 /* agp memory management */
241 int sis_ioctl_agp_init( DRM_IOCTL_ARGS )
244 drm_sis_private_t *dev_priv = dev->dev_private;
247 if (dev_priv == NULL) {
248 dev->dev_private = DRM(calloc)(1, sizeof(drm_sis_private_t),
250 dev_priv = dev->dev_private;
251 if (dev_priv == NULL)
255 if (dev_priv->AGPHeap != NULL)
256 return DRM_ERR(EINVAL);
258 DRM_COPY_FROM_USER_IOCTL(agp, (drm_sis_agp_t *)data, sizeof(agp));
260 dev_priv->AGPHeap = mmInit(agp.offset, agp.size);
262 DRM_DEBUG("offset = %u, size = %u", agp.offset, agp.size);
267 int sis_ioctl_agp_alloc( DRM_IOCTL_ARGS )
270 drm_sis_private_t *dev_priv = dev->dev_private;
275 if (dev_priv == NULL || dev_priv->AGPHeap == NULL)
276 return DRM_ERR(EINVAL);
278 DRM_COPY_FROM_USER_IOCTL(agp, (drm_sis_mem_t *)data, sizeof(agp));
280 block = mmAllocMem(dev_priv->AGPHeap, agp.size, 0, 0);
283 agp.offset = block->ofs;
284 agp.free = (unsigned long)block;
285 if (!add_alloc_set(agp.context, AGP_TYPE, agp.free)) {
286 DRM_DEBUG("adding to allocation set fails\n");
287 mmFreeMem((PMemBlock)agp.free);
296 DRM_COPY_TO_USER_IOCTL((drm_sis_mem_t *)data, agp, sizeof(agp));
298 DRM_DEBUG("alloc agp, size = %d, offset = %d\n", agp.size, agp.offset);
303 int sis_ioctl_agp_free( DRM_IOCTL_ARGS )
306 drm_sis_private_t *dev_priv = dev->dev_private;
309 if (dev_priv == NULL || dev_priv->AGPHeap == NULL)
310 return DRM_ERR(EINVAL);
312 DRM_COPY_FROM_USER_IOCTL(agp, (drm_sis_mem_t *)data, sizeof(agp));
314 if (!mmBlockInHeap(dev_priv->AGPHeap, (PMemBlock)agp.free))
315 return DRM_ERR(EINVAL);
317 mmFreeMem((PMemBlock)agp.free);
318 if (!del_alloc_set(agp.context, AGP_TYPE, agp.free))
319 return DRM_ERR(EINVAL);
321 DRM_DEBUG("free agp, free = 0x%lx\n", agp.free);
326 int sis_init_context(int context)
330 for (i = 0; i < MAX_CONTEXT ; i++) {
331 if (global_ppriv[i].used &&
332 (global_ppriv[i].context == context))
336 if (i >= MAX_CONTEXT) {
337 for (i = 0; i < MAX_CONTEXT ; i++) {
338 if (!global_ppriv[i].used) {
339 global_ppriv[i].context = context;
340 global_ppriv[i].used = 1;
341 global_ppriv[i].sets[0] = setInit();
342 global_ppriv[i].sets[1] = setInit();
343 DRM_DEBUG("init allocation set, socket=%d, "
344 "context = %d\n", i, context);
348 if ((i >= MAX_CONTEXT) || (global_ppriv[i].sets[0] == NULL) ||
349 (global_ppriv[i].sets[1] == NULL))
358 int sis_final_context(int context)
362 for (i=0; i<MAX_CONTEXT; i++) {
363 if (global_ppriv[i].used &&
364 (global_ppriv[i].context == context))
368 if (i < MAX_CONTEXT) {
373 DRM_DEBUG("find socket %d, context = %d\n", i, context);
376 set = global_ppriv[i].sets[0];
377 retval = setFirst(set, &item);
379 DRM_DEBUG("free video memory 0x%x\n", item);
380 #if defined(__linux__) && defined(CONFIG_FB_SIS)
383 mmFreeMem((PMemBlock)item);
385 retval = setNext(set, &item);
390 set = global_ppriv[i].sets[1];
391 retval = setFirst(set, &item);
393 DRM_DEBUG("free agp memory 0x%x\n", item);
394 mmFreeMem((PMemBlock)item);
395 retval = setNext(set, &item);
399 global_ppriv[i].used = 0;