mga_context.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:6k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /* mga_context.c -- IOCTLs for mga contexts -*- linux-c -*-
  2.  * Created: Mon Dec 13 09:51:35 1999 by faith@precisioninsight.com
  3.  *
  4.  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
  5.  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
  6.  * All Rights Reserved.
  7.  *
  8.  * Permission is hereby granted, free of charge, to any person obtaining a
  9.  * copy of this software and associated documentation files (the "Software"),
  10.  * to deal in the Software without restriction, including without limitation
  11.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  12.  * and/or sell copies of the Software, and to permit persons to whom the
  13.  * Software is furnished to do so, subject to the following conditions:
  14.  * 
  15.  * The above copyright notice and this permission notice (including the next
  16.  * paragraph) shall be included in all copies or substantial portions of the
  17.  * Software.
  18.  * 
  19.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  22.  * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  23.  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  24.  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  25.  * DEALINGS IN THE SOFTWARE.
  26.  * 
  27.  * Author: Rickard E. (Rik) Faith <faith@valinux.com>
  28.  *    Jeff Hartmann <jhartmann@valinux.com>
  29.  *
  30.  */
  31. #define __NO_VERSION__
  32. #include "drmP.h"
  33. #include "mga_drv.h"
  34. static int mga_alloc_queue(drm_device_t *dev)
  35. {
  36.     return drm_ctxbitmap_next(dev);
  37. }
  38. int mga_context_switch(drm_device_t *dev, int old, int new)
  39. {
  40.         char        buf[64];
  41.         atomic_inc(&dev->total_ctx);
  42.         if (test_and_set_bit(0, &dev->context_flag)) {
  43.                 DRM_ERROR("Reentering -- FIXMEn");
  44.                 return -EBUSY;
  45.         }
  46. #if DRM_DMA_HISTOGRAM
  47.         dev->ctx_start = get_cycles();
  48. #endif
  49.         
  50.         DRM_DEBUG("Context switch from %d to %dn", old, new);
  51.         if (new == dev->last_context) {
  52.                 clear_bit(0, &dev->context_flag);
  53.                 return 0;
  54.         }
  55.         
  56.         if (drm_flags & DRM_FLAG_NOCTX) {
  57.                 mga_context_switch_complete(dev, new);
  58.         } else {
  59.                 sprintf(buf, "C %d %dn", old, new);
  60.                 drm_write_string(dev, buf);
  61.         }
  62.         
  63.         return 0;
  64. }
  65. int mga_context_switch_complete(drm_device_t *dev, int new)
  66. {
  67.         dev->last_context = new;  /* PRE/POST: This is the _only_ writer. */
  68.         dev->last_switch  = jiffies;
  69.         
  70.         if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock)) {
  71.                 DRM_ERROR("Lock isn't held after context switchn");
  72.         }
  73. /* If a context switch is ever initiated
  74.                                    when the kernel holds the lock, release
  75.                                    that lock here. */
  76. #if DRM_DMA_HISTOGRAM
  77.         atomic_inc(&dev->histo.ctx[drm_histogram_slot(get_cycles()
  78.                                                       - dev->ctx_start)]);
  79.                    
  80. #endif
  81.         clear_bit(0, &dev->context_flag);
  82.         wake_up(&dev->context_wait);
  83.         
  84.         return 0;
  85. }
  86. int mga_resctx(struct inode *inode, struct file *filp, unsigned int cmd,
  87.        unsigned long arg)
  88. {
  89. drm_ctx_res_t res;
  90. drm_ctx_t ctx;
  91. int i;
  92. if (copy_from_user(&res, (drm_ctx_res_t *)arg, sizeof(res)))
  93. return -EFAULT;
  94. if (res.count >= DRM_RESERVED_CONTEXTS) {
  95. memset(&ctx, 0, sizeof(ctx));
  96. for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
  97. ctx.handle = i;
  98. if (copy_to_user(&res.contexts[i],
  99.  &i,
  100.  sizeof(i)))
  101. return -EFAULT;
  102. }
  103. }
  104. res.count = DRM_RESERVED_CONTEXTS;
  105. if (copy_to_user((drm_ctx_res_t *)arg, &res, sizeof(res)))
  106. return -EFAULT;
  107. return 0;
  108. }
  109. int mga_addctx(struct inode *inode, struct file *filp, unsigned int cmd,
  110.        unsigned long arg)
  111. {
  112. drm_file_t *priv = filp->private_data;
  113. drm_device_t *dev = priv->dev;
  114. drm_ctx_t ctx;
  115. if (copy_from_user(&ctx, (drm_ctx_t *)arg, sizeof(ctx)))
  116. return -EFAULT;
  117. if ((ctx.handle = mga_alloc_queue(dev)) == DRM_KERNEL_CONTEXT) {
  118. /* Skip kernel's context and get a new one. */
  119. ctx.handle = mga_alloc_queue(dev);
  120. }
  121.         if (ctx.handle == -1) {
  122. return -ENOMEM;
  123. }
  124. DRM_DEBUG("%dn", ctx.handle);
  125. if (copy_to_user((drm_ctx_t *)arg, &ctx, sizeof(ctx)))
  126. return -EFAULT;
  127. return 0;
  128. }
  129. int mga_modctx(struct inode *inode, struct file *filp, unsigned int cmd,
  130. unsigned long arg)
  131. {
  132.     /* This does nothing for the mga */
  133. return 0;
  134. }
  135. int mga_getctx(struct inode *inode, struct file *filp, unsigned int cmd,
  136. unsigned long arg)
  137. {
  138. drm_ctx_t ctx;
  139. if (copy_from_user(&ctx, (drm_ctx_t*)arg, sizeof(ctx)))
  140. return -EFAULT;
  141. /* This is 0, because we don't hanlde any context flags */
  142. ctx.flags = 0;
  143. if (copy_to_user((drm_ctx_t*)arg, &ctx, sizeof(ctx)))
  144. return -EFAULT;
  145. return 0;
  146. }
  147. int mga_switchctx(struct inode *inode, struct file *filp, unsigned int cmd,
  148.    unsigned long arg)
  149. {
  150. drm_file_t *priv = filp->private_data;
  151. drm_device_t *dev = priv->dev;
  152. drm_ctx_t ctx;
  153. if (copy_from_user(&ctx, (drm_ctx_t *)arg, sizeof(ctx)))
  154. return -EFAULT;
  155. DRM_DEBUG("%dn", ctx.handle);
  156. return mga_context_switch(dev, dev->last_context, ctx.handle);
  157. }
  158. int mga_newctx(struct inode *inode, struct file *filp, unsigned int cmd,
  159. unsigned long arg)
  160. {
  161. drm_file_t *priv = filp->private_data;
  162. drm_device_t *dev = priv->dev;
  163. drm_ctx_t ctx;
  164. if (copy_from_user(&ctx, (drm_ctx_t *)arg, sizeof(ctx)))
  165. return -EFAULT;
  166. DRM_DEBUG("%dn", ctx.handle);
  167. mga_context_switch_complete(dev, ctx.handle);
  168. return 0;
  169. }
  170. int mga_rmctx(struct inode *inode, struct file *filp, unsigned int cmd,
  171.       unsigned long arg)
  172. {
  173. drm_file_t *priv = filp->private_data;
  174. drm_device_t *dev = priv->dev;
  175. drm_ctx_t ctx;
  176. if (copy_from_user(&ctx, (drm_ctx_t *)arg, sizeof(ctx)))
  177. return -EFAULT;
  178. DRM_DEBUG("%dn", ctx.handle);
  179. if(ctx.handle == DRM_KERNEL_CONTEXT+1) priv->remove_auth_on_close = 1;
  180.        if(ctx.handle != DRM_KERNEL_CONTEXT) {
  181.     drm_ctxbitmap_free(dev, ctx.handle);
  182. }
  183. return 0;
  184. }