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 drm_ioctl_desc_t sis_ioctls[] = {
40 [DRM_IOCTL_NR(DRM_SIS_FB_ALLOC)] = { sis_fb_alloc, 1, 0 },
41 [DRM_IOCTL_NR(DRM_SIS_FB_FREE)] = { sis_fb_free, 1, 0 },
42 [DRM_IOCTL_NR(DRM_SIS_AGP_INIT)] = { sis_ioctl_agp_init, 1, 1 },
43 [DRM_IOCTL_NR(DRM_SIS_AGP_ALLOC)] = { sis_ioctl_agp_alloc, 1, 0 },
44 [DRM_IOCTL_NR(DRM_SIS_AGP_FREE)] = { sis_ioctl_agp_free, 1, 0 },
45 [DRM_IOCTL_NR(DRM_SIS_FB_INIT)] = { sis_fb_init, 1, 1 }
48 int sis_max_ioctl = DRM_ARRAY_SIZE(sis_ioctls);
50 #define MAX_CONTEXT 100
57 set_t *sets[2]; /* 0 for video, 1 for AGP */
60 static sis_context_t global_ppriv[MAX_CONTEXT];
63 static int add_alloc_set(int context, int type, unsigned int val)
67 for (i = 0; i < MAX_CONTEXT; i++) {
68 if (global_ppriv[i].used && global_ppriv[i].context == context)
70 retval = setAdd(global_ppriv[i].sets[type], val);
77 static int del_alloc_set(int context, int type, unsigned int val)
81 for (i = 0; i < MAX_CONTEXT; i++) {
82 if (global_ppriv[i].used && global_ppriv[i].context == context)
84 retval = setDel(global_ppriv[i].sets[type], val);
91 /* fb management via fb device */
92 #if defined(__linux__) && defined(CONFIG_FB_SIS)
94 int sis_fb_init( DRM_IOCTL_ARGS )
99 int sis_fb_alloc( DRM_IOCTL_ARGS )
102 struct sis_memreq req;
103 drm_sis_mem_t __user *argp = (void __user *)data;
106 DRM_COPY_FROM_USER_IOCTL(fb, argp, sizeof(fb));
112 fb.offset = req.offset;
113 fb.free = req.offset;
114 if (!add_alloc_set(fb.context, VIDEO_TYPE, fb.free)) {
115 DRM_DEBUG("adding to allocation set fails\n");
116 sis_free(req.offset);
117 retval = DRM_ERR(EINVAL);
125 DRM_COPY_TO_USER_IOCTL(argp, fb, sizeof(fb));
127 DRM_DEBUG("alloc fb, size = %d, offset = %d\n", fb.size, req.offset);
132 int sis_fb_free( DRM_IOCTL_ARGS )
137 DRM_COPY_FROM_USER_IOCTL(fb, (drm_sis_mem_t __user *)data, sizeof(fb));
140 return DRM_ERR(EINVAL);
142 if (!del_alloc_set(fb.context, VIDEO_TYPE, fb.free))
143 retval = DRM_ERR(EINVAL);
144 sis_free((u32)fb.free);
146 DRM_DEBUG("free fb, offset = %lu\n", fb.free);
153 /* Called by the X Server to initialize the FB heap. Allocations will fail
154 * unless this is called. Offset is the beginning of the heap from the
155 * framebuffer offset (MaxXFBMem in XFree86).
157 * Memory layout according to Thomas Winischofer:
158 * |------------------|DDDDDDDDDDDDDDDDDDDDDDDDDDDDD|HHHH|CCCCCCCCCCC|
160 * X driver/sisfb HW- Command-
161 * framebuffer memory DRI heap Cursor queue
163 int sis_fb_init( DRM_IOCTL_ARGS )
166 drm_sis_private_t *dev_priv = dev->dev_private;
169 DRM_COPY_FROM_USER_IOCTL(fb, (drm_sis_fb_t __user *)data, sizeof(fb));
171 if (dev_priv == NULL) {
172 dev->dev_private = drm_calloc(1, sizeof(drm_sis_private_t),
174 dev_priv = dev->dev_private;
175 if (dev_priv == NULL)
179 if (dev_priv->FBHeap != NULL)
180 return DRM_ERR(EINVAL);
182 dev_priv->FBHeap = mmInit(fb.offset, fb.size);
184 DRM_DEBUG("offset = %u, size = %u", fb.offset, fb.size);
189 int sis_fb_alloc( DRM_IOCTL_ARGS )
192 drm_sis_private_t *dev_priv = dev->dev_private;
193 drm_sis_mem_t __user *argp = (void __user *)data;
198 if (dev_priv == NULL || dev_priv->FBHeap == NULL)
199 return DRM_ERR(EINVAL);
201 DRM_COPY_FROM_USER_IOCTL(fb, argp, sizeof(fb));
203 block = mmAllocMem(dev_priv->FBHeap, fb.size, 0, 0);
206 fb.offset = block->ofs;
207 fb.free = (unsigned long)block;
208 if (!add_alloc_set(fb.context, VIDEO_TYPE, fb.free)) {
209 DRM_DEBUG("adding to allocation set fails\n");
210 mmFreeMem((PMemBlock)fb.free);
211 retval = DRM_ERR(EINVAL);
219 DRM_COPY_TO_USER_IOCTL(argp, fb, sizeof(fb));
221 DRM_DEBUG("alloc fb, size = %d, offset = %d\n", fb.size, fb.offset);
226 int sis_fb_free( DRM_IOCTL_ARGS )
229 drm_sis_private_t *dev_priv = dev->dev_private;
232 if (dev_priv == NULL || dev_priv->FBHeap == NULL)
233 return DRM_ERR(EINVAL);
235 DRM_COPY_FROM_USER_IOCTL(fb, (drm_sis_mem_t __user *)data, sizeof(fb));
237 if (!mmBlockInHeap(dev_priv->FBHeap, (PMemBlock)fb.free))
238 return DRM_ERR(EINVAL);
240 if (!del_alloc_set(fb.context, VIDEO_TYPE, fb.free))
241 return DRM_ERR(EINVAL);
242 mmFreeMem((PMemBlock)fb.free);
244 DRM_DEBUG("free fb, free = 0x%lx\n", fb.free);
251 /* agp memory management */
253 int sis_ioctl_agp_init( DRM_IOCTL_ARGS )
256 drm_sis_private_t *dev_priv = dev->dev_private;
259 if (dev_priv == NULL) {
260 dev->dev_private = drm_calloc(1, sizeof(drm_sis_private_t),
262 dev_priv = dev->dev_private;
263 if (dev_priv == NULL)
267 if (dev_priv->AGPHeap != NULL)
268 return DRM_ERR(EINVAL);
270 DRM_COPY_FROM_USER_IOCTL(agp, (drm_sis_agp_t __user *)data, sizeof(agp));
272 dev_priv->AGPHeap = mmInit(agp.offset, agp.size);
274 DRM_DEBUG("offset = %u, size = %u", agp.offset, agp.size);
279 int sis_ioctl_agp_alloc( DRM_IOCTL_ARGS )
282 drm_sis_private_t *dev_priv = dev->dev_private;
283 drm_sis_mem_t __user *argp = (void __user *)data;
288 if (dev_priv == NULL || dev_priv->AGPHeap == NULL)
289 return DRM_ERR(EINVAL);
291 DRM_COPY_FROM_USER_IOCTL(agp, argp, sizeof(agp));
293 block = mmAllocMem(dev_priv->AGPHeap, agp.size, 0, 0);
296 agp.offset = block->ofs;
297 agp.free = (unsigned long)block;
298 if (!add_alloc_set(agp.context, AGP_TYPE, agp.free)) {
299 DRM_DEBUG("adding to allocation set fails\n");
300 mmFreeMem((PMemBlock)agp.free);
309 DRM_COPY_TO_USER_IOCTL(argp, agp, sizeof(agp));
311 DRM_DEBUG("alloc agp, size = %d, offset = %d\n", agp.size, agp.offset);
316 int sis_ioctl_agp_free( DRM_IOCTL_ARGS )
319 drm_sis_private_t *dev_priv = dev->dev_private;
322 if (dev_priv == NULL || dev_priv->AGPHeap == NULL)
323 return DRM_ERR(EINVAL);
325 DRM_COPY_FROM_USER_IOCTL(agp, (drm_sis_mem_t __user *)data, sizeof(agp));
327 if (!mmBlockInHeap(dev_priv->AGPHeap, (PMemBlock)agp.free))
328 return DRM_ERR(EINVAL);
330 mmFreeMem((PMemBlock)agp.free);
331 if (!del_alloc_set(agp.context, AGP_TYPE, agp.free))
332 return DRM_ERR(EINVAL);
334 DRM_DEBUG("free agp, free = 0x%lx\n", agp.free);
339 int sis_init_context(struct drm_device *dev, int context)
343 for (i = 0; i < MAX_CONTEXT ; i++) {
344 if (global_ppriv[i].used &&
345 (global_ppriv[i].context == context))
349 if (i >= MAX_CONTEXT) {
350 for (i = 0; i < MAX_CONTEXT ; i++) {
351 if (!global_ppriv[i].used) {
352 global_ppriv[i].context = context;
353 global_ppriv[i].used = 1;
354 global_ppriv[i].sets[0] = setInit();
355 global_ppriv[i].sets[1] = setInit();
356 DRM_DEBUG("init allocation set, socket=%d, "
357 "context = %d\n", i, context);
361 if ((i >= MAX_CONTEXT) || (global_ppriv[i].sets[0] == NULL) ||
362 (global_ppriv[i].sets[1] == NULL))
371 int sis_final_context(struct drm_device *dev, int context)
375 for (i=0; i<MAX_CONTEXT; i++) {
376 if (global_ppriv[i].used &&
377 (global_ppriv[i].context == context))
381 if (i < MAX_CONTEXT) {
386 DRM_DEBUG("find socket %d, context = %d\n", i, context);
389 set = global_ppriv[i].sets[0];
390 retval = setFirst(set, &item);
392 DRM_DEBUG("free video memory 0x%x\n", item);
393 #if defined(__linux__) && defined(CONFIG_FB_SIS)
396 mmFreeMem((PMemBlock)item);
398 retval = setNext(set, &item);
403 set = global_ppriv[i].sets[1];
404 retval = setFirst(set, &item);
406 DRM_DEBUG("free agp memory 0x%x\n", item);
407 mmFreeMem((PMemBlock)item);
408 retval = setNext(set, &item);
412 global_ppriv[i].used = 0;