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>
35 #if defined(__linux__) && defined(CONFIG_FB_SIS)
36 #include <video/sisfb.h>
39 #define MAX_CONTEXT 100
46 set_t *sets[2]; /* 0 for video, 1 for AGP */
49 static sis_context_t global_ppriv[MAX_CONTEXT];
52 static int add_alloc_set(int context, int type, unsigned int val)
56 for (i = 0; i < MAX_CONTEXT; i++) {
57 if (global_ppriv[i].used && global_ppriv[i].context == context)
59 retval = setAdd(global_ppriv[i].sets[type], val);
66 static int del_alloc_set(int context, int type, unsigned int val)
70 for (i = 0; i < MAX_CONTEXT; i++) {
71 if (global_ppriv[i].used && global_ppriv[i].context == context)
73 retval = setDel(global_ppriv[i].sets[type], val);
80 /* fb management via fb device */
81 #if defined(__linux__) && defined(CONFIG_FB_SIS)
83 static int sis_fb_init( DRM_IOCTL_ARGS )
88 static int sis_fb_alloc( DRM_IOCTL_ARGS )
91 struct sis_memreq req;
92 drm_sis_mem_t __user *argp = (void __user *)data;
95 DRM_COPY_FROM_USER_IOCTL(fb, argp, 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(argp, fb, sizeof(fb));
116 DRM_DEBUG("alloc fb, size = %d, offset = %d\n", fb.size, req.offset);
121 static int sis_fb_free( DRM_IOCTL_ARGS )
126 DRM_COPY_FROM_USER_IOCTL(fb, (drm_sis_mem_t __user *)data, sizeof(fb));
129 return DRM_ERR(EINVAL);
131 if (!del_alloc_set(fb.context, VIDEO_TYPE, fb.free))
132 retval = DRM_ERR(EINVAL);
133 sis_free((u32)fb.free);
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 static 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 __user *)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 static int sis_fb_alloc( DRM_IOCTL_ARGS )
181 drm_sis_private_t *dev_priv = dev->dev_private;
182 drm_sis_mem_t __user *argp = (void __user *)data;
187 if (dev_priv == NULL || dev_priv->FBHeap == NULL)
188 return DRM_ERR(EINVAL);
190 DRM_COPY_FROM_USER_IOCTL(fb, argp, sizeof(fb));
192 block = mmAllocMem(dev_priv->FBHeap, fb.size, 0, 0);
195 fb.offset = block->ofs;
196 fb.free = (unsigned long)block;
197 if (!add_alloc_set(fb.context, VIDEO_TYPE, fb.free)) {
198 DRM_DEBUG("adding to allocation set fails\n");
199 mmFreeMem((PMemBlock)fb.free);
200 retval = DRM_ERR(EINVAL);
208 DRM_COPY_TO_USER_IOCTL(argp, fb, sizeof(fb));
210 DRM_DEBUG("alloc fb, size = %d, offset = %d\n", fb.size, fb.offset);
215 static int sis_fb_free( DRM_IOCTL_ARGS )
218 drm_sis_private_t *dev_priv = dev->dev_private;
221 if (dev_priv == NULL || dev_priv->FBHeap == NULL)
222 return DRM_ERR(EINVAL);
224 DRM_COPY_FROM_USER_IOCTL(fb, (drm_sis_mem_t __user *)data, sizeof(fb));
226 if (!mmBlockInHeap(dev_priv->FBHeap, (PMemBlock)fb.free))
227 return DRM_ERR(EINVAL);
229 if (!del_alloc_set(fb.context, VIDEO_TYPE, fb.free))
230 return DRM_ERR(EINVAL);
231 mmFreeMem((PMemBlock)fb.free);
233 DRM_DEBUG("free fb, free = 0x%lx\n", fb.free);
240 /* agp memory management */
242 static int sis_ioctl_agp_init( DRM_IOCTL_ARGS )
245 drm_sis_private_t *dev_priv = dev->dev_private;
248 if (dev_priv == NULL) {
249 dev->dev_private = drm_calloc(1, sizeof(drm_sis_private_t),
251 dev_priv = dev->dev_private;
252 if (dev_priv == NULL)
256 if (dev_priv->AGPHeap != NULL)
257 return DRM_ERR(EINVAL);
259 DRM_COPY_FROM_USER_IOCTL(agp, (drm_sis_agp_t __user *)data, sizeof(agp));
261 dev_priv->AGPHeap = mmInit(agp.offset, agp.size);
263 DRM_DEBUG("offset = %u, size = %u", agp.offset, agp.size);
268 static int sis_ioctl_agp_alloc( DRM_IOCTL_ARGS )
271 drm_sis_private_t *dev_priv = dev->dev_private;
272 drm_sis_mem_t __user *argp = (void __user *)data;
277 if (dev_priv == NULL || dev_priv->AGPHeap == NULL)
278 return DRM_ERR(EINVAL);
280 DRM_COPY_FROM_USER_IOCTL(agp, argp, sizeof(agp));
282 block = mmAllocMem(dev_priv->AGPHeap, agp.size, 0, 0);
285 agp.offset = block->ofs;
286 agp.free = (unsigned long)block;
287 if (!add_alloc_set(agp.context, AGP_TYPE, agp.free)) {
288 DRM_DEBUG("adding to allocation set fails\n");
289 mmFreeMem((PMemBlock)agp.free);
298 DRM_COPY_TO_USER_IOCTL(argp, agp, sizeof(agp));
300 DRM_DEBUG("alloc agp, size = %d, offset = %d\n", agp.size, agp.offset);
305 static int sis_ioctl_agp_free( DRM_IOCTL_ARGS )
308 drm_sis_private_t *dev_priv = dev->dev_private;
311 if (dev_priv == NULL || dev_priv->AGPHeap == NULL)
312 return DRM_ERR(EINVAL);
314 DRM_COPY_FROM_USER_IOCTL(agp, (drm_sis_mem_t __user *)data, sizeof(agp));
316 if (!mmBlockInHeap(dev_priv->AGPHeap, (PMemBlock)agp.free))
317 return DRM_ERR(EINVAL);
319 mmFreeMem((PMemBlock)agp.free);
320 if (!del_alloc_set(agp.context, AGP_TYPE, agp.free))
321 return DRM_ERR(EINVAL);
323 DRM_DEBUG("free agp, free = 0x%lx\n", agp.free);
328 int sis_init_context(struct drm_device *dev, int context)
332 for (i = 0; i < MAX_CONTEXT ; i++) {
333 if (global_ppriv[i].used &&
334 (global_ppriv[i].context == context))
338 if (i >= MAX_CONTEXT) {
339 for (i = 0; i < MAX_CONTEXT ; i++) {
340 if (!global_ppriv[i].used) {
341 global_ppriv[i].context = context;
342 global_ppriv[i].used = 1;
343 global_ppriv[i].sets[0] = setInit();
344 global_ppriv[i].sets[1] = setInit();
345 DRM_DEBUG("init allocation set, socket=%d, "
346 "context = %d\n", i, context);
350 if ((i >= MAX_CONTEXT) || (global_ppriv[i].sets[0] == NULL) ||
351 (global_ppriv[i].sets[1] == NULL))
360 int sis_final_context(struct drm_device *dev, int context)
364 for (i=0; i<MAX_CONTEXT; i++) {
365 if (global_ppriv[i].used &&
366 (global_ppriv[i].context == context))
370 if (i < MAX_CONTEXT) {
375 DRM_DEBUG("find socket %d, context = %d\n", i, context);
378 set = global_ppriv[i].sets[0];
379 retval = setFirst(set, &item);
381 DRM_DEBUG("free video memory 0x%x\n", item);
382 #if defined(__linux__) && defined(CONFIG_FB_SIS)
385 mmFreeMem((PMemBlock)item);
387 retval = setNext(set, &item);
392 set = global_ppriv[i].sets[1];
393 retval = setFirst(set, &item);
395 DRM_DEBUG("free agp memory 0x%x\n", item);
396 mmFreeMem((PMemBlock)item);
397 retval = setNext(set, &item);
401 global_ppriv[i].used = 0;
407 drm_ioctl_desc_t sis_ioctls[] = {
408 [DRM_IOCTL_NR(DRM_SIS_FB_ALLOC)] = { sis_fb_alloc, 1, 0 },
409 [DRM_IOCTL_NR(DRM_SIS_FB_FREE)] = { sis_fb_free, 1, 0 },
410 [DRM_IOCTL_NR(DRM_SIS_AGP_INIT)] = { sis_ioctl_agp_init, 1, 1 },
411 [DRM_IOCTL_NR(DRM_SIS_AGP_ALLOC)] = { sis_ioctl_agp_alloc, 1, 0 },
412 [DRM_IOCTL_NR(DRM_SIS_AGP_FREE)] = { sis_ioctl_agp_free, 1, 0 },
413 [DRM_IOCTL_NR(DRM_SIS_FB_INIT)] = { sis_fb_init, 1, 1 }
416 int sis_max_ioctl = DRM_ARRAY_SIZE(sis_ioctls);