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>
32 #if defined(__linux__) && defined(CONFIG_FB_SIS)
33 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
34 #include <video/sisfb.h>
36 #include <linux/sisfb.h>
39 #include "dev/drm/drmP.h"
40 #include "dev/drm/sis_drm.h"
41 #include "dev/drm/sis_drv.h"
42 #include "dev/drm/sis_ds.h"
44 #define MAX_CONTEXT 100
51 set_t *sets[2]; /* 0 for video, 1 for AGP */
54 static sis_context_t global_ppriv[MAX_CONTEXT];
56 static int add_alloc_set(int context, int type, unsigned int val)
60 for (i = 0; i < MAX_CONTEXT; i++) {
61 if (global_ppriv[i].used && global_ppriv[i].context == context) {
62 retval = setAdd(global_ppriv[i].sets[type], val);
69 static int del_alloc_set(int context, int type, unsigned int val)
73 for (i = 0; i < MAX_CONTEXT; i++) {
74 if (global_ppriv[i].used && global_ppriv[i].context == context) {
75 retval = setDel(global_ppriv[i].sets[type], val);
82 /* fb management via fb device */
83 #if defined(__linux__) && defined(CONFIG_FB_SIS)
85 static int sis_fb_init(DRM_IOCTL_ARGS)
90 static int sis_fb_alloc(DRM_IOCTL_ARGS)
93 struct sis_memreq req;
94 drm_sis_mem_t __user *argp = (void __user *)data;
97 DRM_COPY_FROM_USER_IOCTL(fb, argp, sizeof(fb));
103 fb.offset = req.offset;
104 fb.free = req.offset;
105 if (!add_alloc_set(fb.context, VIDEO_TYPE, fb.free)) {
106 DRM_DEBUG("adding to allocation set fails\n");
107 sis_free(req.offset);
108 retval = DRM_ERR(EINVAL);
116 DRM_COPY_TO_USER_IOCTL(argp, fb, sizeof(fb));
118 DRM_DEBUG("alloc fb, size = %d, offset = %ld\n", fb.size, req.offset);
123 static int sis_fb_free(DRM_IOCTL_ARGS)
128 DRM_COPY_FROM_USER_IOCTL(fb, (drm_sis_mem_t __user *) data, sizeof(fb));
131 return DRM_ERR(EINVAL);
133 if (!del_alloc_set(fb.context, VIDEO_TYPE, fb.free))
134 retval = DRM_ERR(EINVAL);
137 DRM_DEBUG("free fb, offset = 0x%lx\n", fb.free);
144 /* Called by the X Server to initialize the FB heap. Allocations will fail
145 * unless this is called. Offset is the beginning of the heap from the
146 * framebuffer offset (MaxXFBMem in XFree86).
148 * Memory layout according to Thomas Winischofer:
149 * |------------------|DDDDDDDDDDDDDDDDDDDDDDDDDDDDD|HHHH|CCCCCCCCCCC|
151 * X driver/sisfb HW- Command-
152 * framebuffer memory DRI heap Cursor queue
154 static int sis_fb_init(DRM_IOCTL_ARGS)
157 drm_sis_private_t *dev_priv = dev->dev_private;
160 DRM_COPY_FROM_USER_IOCTL(fb, (drm_sis_fb_t __user *) data, sizeof(fb));
162 if (dev_priv == NULL) {
163 dev->dev_private = drm_calloc(1, sizeof(drm_sis_private_t),
165 dev_priv = dev->dev_private;
166 if (dev_priv == NULL)
170 if (dev_priv->FBHeap != NULL)
171 return DRM_ERR(EINVAL);
173 dev_priv->FBHeap = mmInit(fb.offset, fb.size);
175 DRM_DEBUG("offset = %u, size = %u", fb.offset, fb.size);
180 static int sis_fb_alloc(DRM_IOCTL_ARGS)
183 drm_sis_private_t *dev_priv = dev->dev_private;
184 drm_sis_mem_t __user *argp = (void __user *)data;
189 if (dev_priv == NULL || dev_priv->FBHeap == NULL)
190 return DRM_ERR(EINVAL);
192 DRM_COPY_FROM_USER_IOCTL(fb, argp, sizeof(fb));
194 block = mmAllocMem(dev_priv->FBHeap, fb.size, 0, 0);
197 fb.offset = block->ofs;
198 fb.free = (unsigned long)block;
199 if (!add_alloc_set(fb.context, VIDEO_TYPE, fb.free)) {
200 DRM_DEBUG("adding to allocation set fails\n");
201 mmFreeMem((PMemBlock) fb.free);
202 retval = DRM_ERR(EINVAL);
210 DRM_COPY_TO_USER_IOCTL(argp, fb, sizeof(fb));
212 DRM_DEBUG("alloc fb, size = %d, offset = %d\n", fb.size, fb.offset);
217 static int sis_fb_free(DRM_IOCTL_ARGS)
220 drm_sis_private_t *dev_priv = dev->dev_private;
223 if (dev_priv == NULL || dev_priv->FBHeap == NULL)
224 return DRM_ERR(EINVAL);
226 DRM_COPY_FROM_USER_IOCTL(fb, (drm_sis_mem_t __user *) data, sizeof(fb));
228 if (!mmBlockInHeap(dev_priv->FBHeap, (PMemBlock) fb.free))
229 return DRM_ERR(EINVAL);
231 if (!del_alloc_set(fb.context, VIDEO_TYPE, fb.free))
232 return DRM_ERR(EINVAL);
233 mmFreeMem((PMemBlock) fb.free);
235 DRM_DEBUG("free fb, free = 0x%lx\n", fb.free);
242 /* agp memory management */
244 static int sis_ioctl_agp_init(DRM_IOCTL_ARGS)
247 drm_sis_private_t *dev_priv = dev->dev_private;
250 if (dev_priv == NULL) {
251 dev->dev_private = drm_calloc(1, sizeof(drm_sis_private_t),
253 dev_priv = dev->dev_private;
254 if (dev_priv == NULL)
258 if (dev_priv->AGPHeap != NULL)
259 return DRM_ERR(EINVAL);
261 DRM_COPY_FROM_USER_IOCTL(agp, (drm_sis_agp_t __user *) data,
264 dev_priv->AGPHeap = mmInit(agp.offset, agp.size);
266 DRM_DEBUG("offset = %u, size = %u", agp.offset, agp.size);
271 static int sis_ioctl_agp_alloc(DRM_IOCTL_ARGS)
274 drm_sis_private_t *dev_priv = dev->dev_private;
275 drm_sis_mem_t __user *argp = (drm_sis_mem_t __user *)data;
280 if (dev_priv == NULL || dev_priv->AGPHeap == NULL)
281 return DRM_ERR(EINVAL);
283 DRM_COPY_FROM_USER_IOCTL(agp, argp, sizeof(agp));
285 block = mmAllocMem(dev_priv->AGPHeap, agp.size, 0, 0);
288 agp.offset = block->ofs;
289 agp.free = (unsigned long)block;
290 if (!add_alloc_set(agp.context, AGP_TYPE, agp.free)) {
291 DRM_DEBUG("adding to allocation set fails\n");
292 mmFreeMem((PMemBlock) agp.free);
301 DRM_COPY_TO_USER_IOCTL(argp, agp, sizeof(agp));
303 DRM_DEBUG("alloc agp, size = %d, offset = %d\n", agp.size, agp.offset);
308 static int sis_ioctl_agp_free(DRM_IOCTL_ARGS)
311 drm_sis_private_t *dev_priv = dev->dev_private;
314 if (dev_priv == NULL || dev_priv->AGPHeap == NULL)
315 return DRM_ERR(EINVAL);
317 DRM_COPY_FROM_USER_IOCTL(agp, (drm_sis_mem_t __user *) data,
320 if (!mmBlockInHeap(dev_priv->AGPHeap, (PMemBlock) agp.free))
321 return DRM_ERR(EINVAL);
323 mmFreeMem((PMemBlock) agp.free);
324 if (!del_alloc_set(agp.context, AGP_TYPE, agp.free))
325 return DRM_ERR(EINVAL);
327 DRM_DEBUG("free agp, free = 0x%lx\n", agp.free);
332 int sis_init_context(struct drm_device *dev, int context)
336 for (i = 0; i < MAX_CONTEXT; i++) {
337 if (global_ppriv[i].used &&
338 (global_ppriv[i].context == context))
342 if (i >= MAX_CONTEXT) {
343 for (i = 0; i < MAX_CONTEXT; i++) {
344 if (!global_ppriv[i].used) {
345 global_ppriv[i].context = context;
346 global_ppriv[i].used = 1;
347 global_ppriv[i].sets[0] = setInit();
348 global_ppriv[i].sets[1] = setInit();
349 DRM_DEBUG("init allocation set, socket=%d, "
350 "context = %d\n", i, context);
354 if ((i >= MAX_CONTEXT) || (global_ppriv[i].sets[0] == NULL) ||
355 (global_ppriv[i].sets[1] == NULL)) {
363 int sis_final_context(struct drm_device *dev, int context)
367 for (i = 0; i < MAX_CONTEXT; i++) {
368 if (global_ppriv[i].used &&
369 (global_ppriv[i].context == context))
373 if (i < MAX_CONTEXT) {
378 DRM_DEBUG("find socket %d, context = %d\n", i, context);
381 set = global_ppriv[i].sets[0];
382 retval = setFirst(set, &item);
384 DRM_DEBUG("free video memory 0x%lx\n", item);
385 #if defined(__linux__) && defined(CONFIG_FB_SIS)
388 mmFreeMem((PMemBlock) item);
390 retval = setNext(set, &item);
395 set = global_ppriv[i].sets[1];
396 retval = setFirst(set, &item);
398 DRM_DEBUG("free agp memory 0x%lx\n", item);
399 mmFreeMem((PMemBlock) item);
400 retval = setNext(set, &item);
404 global_ppriv[i].used = 0;
410 drm_ioctl_desc_t sis_ioctls[] = {
411 [DRM_IOCTL_NR(DRM_SIS_FB_ALLOC)] = {sis_fb_alloc, 1, 0},
412 [DRM_IOCTL_NR(DRM_SIS_FB_FREE)] = {sis_fb_free, 1, 0},
413 [DRM_IOCTL_NR(DRM_SIS_AGP_INIT)] = {sis_ioctl_agp_init, 1, 1},
414 [DRM_IOCTL_NR(DRM_SIS_AGP_ALLOC)] = {sis_ioctl_agp_alloc, 1, 0},
415 [DRM_IOCTL_NR(DRM_SIS_AGP_FREE)] = {sis_ioctl_agp_free, 1, 0},
416 [DRM_IOCTL_NR(DRM_SIS_FB_INIT)] = {sis_fb_init, 1, 1}
419 int sis_max_ioctl = DRM_ARRAY_SIZE(sis_ioctls);