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];
 
  51 static int add_alloc_set(int context, int type, unsigned int val)
 
  55         for (i = 0; i < MAX_CONTEXT; i++) {
 
  56                 if (global_ppriv[i].used && global_ppriv[i].context == context) {
 
  57                         retval = setAdd(global_ppriv[i].sets[type], val);
 
  64 static int del_alloc_set(int context, int type, unsigned int val)
 
  68         for (i = 0; i < MAX_CONTEXT; i++) {
 
  69                 if (global_ppriv[i].used && global_ppriv[i].context == context) {
 
  70                         retval = setDel(global_ppriv[i].sets[type], val);
 
  77 /* fb management via fb device */
 
  78 #if defined(__linux__) && defined(CONFIG_FB_SIS)
 
  80 static int sis_fb_init(DRM_IOCTL_ARGS)
 
  85 static int sis_fb_alloc(DRM_IOCTL_ARGS)
 
  88         struct sis_memreq req;
 
  89         drm_sis_mem_t __user *argp = (void __user *)data;
 
  92         DRM_COPY_FROM_USER_IOCTL(fb, argp, sizeof(fb));
 
  98                 fb.offset = req.offset;
 
 100                 if (!add_alloc_set(fb.context, VIDEO_TYPE, fb.free)) {
 
 101                         DRM_DEBUG("adding to allocation set fails\n");
 
 102                         sis_free(req.offset);
 
 103                         retval = DRM_ERR(EINVAL);
 
 111         DRM_COPY_TO_USER_IOCTL(argp, fb, sizeof(fb));
 
 113         DRM_DEBUG("alloc fb, size = %d, offset = %d\n", fb.size, req.offset);
 
 118 static int sis_fb_free(DRM_IOCTL_ARGS)
 
 123         DRM_COPY_FROM_USER_IOCTL(fb, (drm_sis_mem_t __user *) data, sizeof(fb));
 
 126                 return DRM_ERR(EINVAL);
 
 128         if (!del_alloc_set(fb.context, VIDEO_TYPE, fb.free))
 
 129                 retval = DRM_ERR(EINVAL);
 
 130         sis_free((u32) fb.free);
 
 132         DRM_DEBUG("free fb, offset = %lu\n", fb.free);
 
 139 /* Called by the X Server to initialize the FB heap.  Allocations will fail
 
 140  * unless this is called.  Offset is the beginning of the heap from the
 
 141  * framebuffer offset (MaxXFBMem in XFree86).
 
 143  * Memory layout according to Thomas Winischofer:
 
 144  * |------------------|DDDDDDDDDDDDDDDDDDDDDDDDDDDDD|HHHH|CCCCCCCCCCC|
 
 146  *    X driver/sisfb                                  HW-   Command-
 
 147  *  framebuffer memory           DRI heap           Cursor   queue
 
 149 static int sis_fb_init(DRM_IOCTL_ARGS)
 
 152         drm_sis_private_t *dev_priv = dev->dev_private;
 
 155         DRM_COPY_FROM_USER_IOCTL(fb, (drm_sis_fb_t __user *) data, sizeof(fb));
 
 157         if (dev_priv == NULL) {
 
 158                 dev->dev_private = drm_calloc(1, sizeof(drm_sis_private_t),
 
 160                 dev_priv = dev->dev_private;
 
 161                 if (dev_priv == NULL)
 
 165         if (dev_priv->FBHeap != NULL)
 
 166                 return DRM_ERR(EINVAL);
 
 168         dev_priv->FBHeap = mmInit(fb.offset, fb.size);
 
 170         DRM_DEBUG("offset = %u, size = %u", fb.offset, fb.size);
 
 175 static int sis_fb_alloc(DRM_IOCTL_ARGS)
 
 178         drm_sis_private_t *dev_priv = dev->dev_private;
 
 179         drm_sis_mem_t __user *argp = (void __user *)data;
 
 184         if (dev_priv == NULL || dev_priv->FBHeap == NULL)
 
 185                 return DRM_ERR(EINVAL);
 
 187         DRM_COPY_FROM_USER_IOCTL(fb, argp, sizeof(fb));
 
 189         block = mmAllocMem(dev_priv->FBHeap, fb.size, 0, 0);
 
 192                 fb.offset = block->ofs;
 
 193                 fb.free = (unsigned long)block;
 
 194                 if (!add_alloc_set(fb.context, VIDEO_TYPE, fb.free)) {
 
 195                         DRM_DEBUG("adding to allocation set fails\n");
 
 196                         mmFreeMem((PMemBlock) fb.free);
 
 197                         retval = DRM_ERR(EINVAL);
 
 205         DRM_COPY_TO_USER_IOCTL(argp, fb, sizeof(fb));
 
 207         DRM_DEBUG("alloc fb, size = %d, offset = %d\n", fb.size, fb.offset);
 
 212 static int sis_fb_free(DRM_IOCTL_ARGS)
 
 215         drm_sis_private_t *dev_priv = dev->dev_private;
 
 218         if (dev_priv == NULL || dev_priv->FBHeap == NULL)
 
 219                 return DRM_ERR(EINVAL);
 
 221         DRM_COPY_FROM_USER_IOCTL(fb, (drm_sis_mem_t __user *) data, sizeof(fb));
 
 223         if (!mmBlockInHeap(dev_priv->FBHeap, (PMemBlock) fb.free))
 
 224                 return DRM_ERR(EINVAL);
 
 226         if (!del_alloc_set(fb.context, VIDEO_TYPE, fb.free))
 
 227                 return DRM_ERR(EINVAL);
 
 228         mmFreeMem((PMemBlock) fb.free);
 
 230         DRM_DEBUG("free fb, free = 0x%lx\n", fb.free);
 
 237 /* agp memory management */
 
 239 static int sis_ioctl_agp_init(DRM_IOCTL_ARGS)
 
 242         drm_sis_private_t *dev_priv = dev->dev_private;
 
 245         if (dev_priv == NULL) {
 
 246                 dev->dev_private = drm_calloc(1, sizeof(drm_sis_private_t),
 
 248                 dev_priv = dev->dev_private;
 
 249                 if (dev_priv == NULL)
 
 253         if (dev_priv->AGPHeap != NULL)
 
 254                 return DRM_ERR(EINVAL);
 
 256         DRM_COPY_FROM_USER_IOCTL(agp, (drm_sis_agp_t __user *) data,
 
 259         dev_priv->AGPHeap = mmInit(agp.offset, agp.size);
 
 261         DRM_DEBUG("offset = %u, size = %u", agp.offset, agp.size);
 
 266 static int sis_ioctl_agp_alloc(DRM_IOCTL_ARGS)
 
 269         drm_sis_private_t *dev_priv = dev->dev_private;
 
 270         drm_sis_mem_t __user *argp = (void __user *)data;
 
 275         if (dev_priv == NULL || dev_priv->AGPHeap == NULL)
 
 276                 return DRM_ERR(EINVAL);
 
 278         DRM_COPY_FROM_USER_IOCTL(agp, argp, 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(argp, agp, sizeof(agp));
 
 298         DRM_DEBUG("alloc agp, size = %d, offset = %d\n", agp.size, agp.offset);
 
 303 static 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 __user *) data,
 
 315         if (!mmBlockInHeap(dev_priv->AGPHeap, (PMemBlock) agp.free))
 
 316                 return DRM_ERR(EINVAL);
 
 318         mmFreeMem((PMemBlock) agp.free);
 
 319         if (!del_alloc_set(agp.context, AGP_TYPE, agp.free))
 
 320                 return DRM_ERR(EINVAL);
 
 322         DRM_DEBUG("free agp, free = 0x%lx\n", agp.free);
 
 327 int sis_init_context(struct drm_device *dev, int context)
 
 331         for (i = 0; i < MAX_CONTEXT; i++) {
 
 332                 if (global_ppriv[i].used &&
 
 333                     (global_ppriv[i].context == context))
 
 337         if (i >= MAX_CONTEXT) {
 
 338                 for (i = 0; i < MAX_CONTEXT; i++) {
 
 339                         if (!global_ppriv[i].used) {
 
 340                                 global_ppriv[i].context = context;
 
 341                                 global_ppriv[i].used = 1;
 
 342                                 global_ppriv[i].sets[0] = setInit();
 
 343                                 global_ppriv[i].sets[1] = setInit();
 
 344                                 DRM_DEBUG("init allocation set, socket=%d, "
 
 345                                           "context = %d\n", i, context);
 
 349                 if ((i >= MAX_CONTEXT) || (global_ppriv[i].sets[0] == NULL) ||
 
 350                     (global_ppriv[i].sets[1] == NULL)) {
 
 358 int sis_final_context(struct drm_device *dev, 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;
 
 405 drm_ioctl_desc_t sis_ioctls[] = {
 
 406         [DRM_IOCTL_NR(DRM_SIS_FB_ALLOC)] = {sis_fb_alloc, 1, 0},
 
 407         [DRM_IOCTL_NR(DRM_SIS_FB_FREE)] = {sis_fb_free, 1, 0},
 
 408         [DRM_IOCTL_NR(DRM_SIS_AGP_INIT)] = {sis_ioctl_agp_init, 1, 1},
 
 409         [DRM_IOCTL_NR(DRM_SIS_AGP_ALLOC)] = {sis_ioctl_agp_alloc, 1, 0},
 
 410         [DRM_IOCTL_NR(DRM_SIS_AGP_FREE)] = {sis_ioctl_agp_free, 1, 0},
 
 411         [DRM_IOCTL_NR(DRM_SIS_FB_INIT)] = {sis_fb_init, 1, 1}
 
 414 int sis_max_ioctl = DRM_ARRAY_SIZE(sis_ioctls);