tdfx_context.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:6k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  1. /* tdfx_context.c -- IOCTLs for tdfx contexts -*- linux-c -*-
  2.  * Created: Thu Oct  7 10:50:22 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.  * Authors:
  28.  *    Rickard E. (Rik) Faith <faith@valinux.com>
  29.  *    Daryll Strauss <daryll@valinux.com>
  30.  * 
  31.  */
  32. #define __NO_VERSION__
  33. #include "drmP.h"
  34. #include "tdfx_drv.h"
  35. extern drm_ctx_t tdfx_res_ctx;
  36. static int tdfx_alloc_queue(drm_device_t *dev)
  37. {
  38. return drm_ctxbitmap_next(dev);
  39. }
  40. int tdfx_context_switch(drm_device_t *dev, int old, int new)
  41. {
  42.         char        buf[64];
  43.         atomic_inc(&dev->total_ctx);
  44.         if (test_and_set_bit(0, &dev->context_flag)) {
  45.                 DRM_ERROR("Reentering -- FIXMEn");
  46.                 return -EBUSY;
  47.         }
  48. #if DRM_DMA_HISTOGRAM
  49.         dev->ctx_start = get_cycles();
  50. #endif
  51.         
  52.         DRM_DEBUG("Context switch from %d to %dn", old, new);
  53.         if (new == dev->last_context) {
  54.                 clear_bit(0, &dev->context_flag);
  55.                 return 0;
  56.         }
  57.         
  58.         if (drm_flags & DRM_FLAG_NOCTX) {
  59.                 tdfx_context_switch_complete(dev, new);
  60.         } else {
  61.                 sprintf(buf, "C %d %dn", old, new);
  62.                 drm_write_string(dev, buf);
  63.         }
  64.         
  65.         return 0;
  66. }
  67. int tdfx_context_switch_complete(drm_device_t *dev, int new)
  68. {
  69.         dev->last_context = new;  /* PRE/POST: This is the _only_ writer. */
  70.         dev->last_switch  = jiffies;
  71.         
  72.         if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock)) {
  73.                 DRM_ERROR("Lock isn't held after context switchn");
  74.         }
  75. /* If a context switch is ever initiated
  76.                                    when the kernel holds the lock, release
  77.                                    that lock here. */
  78. #if DRM_DMA_HISTOGRAM
  79.         atomic_inc(&dev->histo.ctx[drm_histogram_slot(get_cycles()
  80.                                                       - dev->ctx_start)]);
  81.                    
  82. #endif
  83.         clear_bit(0, &dev->context_flag);
  84.         wake_up(&dev->context_wait);
  85.         
  86.         return 0;
  87. }
  88. int tdfx_resctx(struct inode *inode, struct file *filp, unsigned int cmd,
  89.        unsigned long arg)
  90. {
  91. drm_ctx_res_t res;
  92. drm_ctx_t ctx;
  93. int i;
  94. DRM_DEBUG("%dn", DRM_RESERVED_CONTEXTS);
  95. if (copy_from_user(&res, (drm_ctx_res_t *)arg, sizeof(res)))
  96. return -EFAULT;
  97. if (res.count >= DRM_RESERVED_CONTEXTS) {
  98. memset(&ctx, 0, sizeof(ctx));
  99. for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
  100. ctx.handle = i;
  101. if (copy_to_user(&res.contexts[i],
  102.  &i,
  103.  sizeof(i)))
  104. return -EFAULT;
  105. }
  106. }
  107. res.count = DRM_RESERVED_CONTEXTS;
  108. if (copy_to_user((drm_ctx_res_t *)arg, &res, sizeof(res)))
  109. return -EFAULT;
  110. return 0;
  111. }
  112. int tdfx_addctx(struct inode *inode, struct file *filp, unsigned int cmd,
  113.        unsigned long arg)
  114. {
  115. drm_file_t *priv = filp->private_data;
  116. drm_device_t *dev = priv->dev;
  117. drm_ctx_t ctx;
  118. if (copy_from_user(&ctx, (drm_ctx_t *)arg, sizeof(ctx)))
  119. return -EFAULT;
  120. if ((ctx.handle = tdfx_alloc_queue(dev)) == DRM_KERNEL_CONTEXT) {
  121. /* Skip kernel's context and get a new one. */
  122. ctx.handle = tdfx_alloc_queue(dev);
  123. }
  124. DRM_DEBUG("%dn", ctx.handle);
  125. if (ctx.handle == -1) {
  126. DRM_DEBUG("Not enough free contexts.n");
  127. /* Should this return -EBUSY instead? */
  128. return -ENOMEM;
  129. }
  130.    
  131. if (copy_to_user((drm_ctx_t *)arg, &ctx, sizeof(ctx)))
  132. return -EFAULT;
  133. return 0;
  134. }
  135. int tdfx_modctx(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. if (ctx.flags==_DRM_CONTEXT_PRESERVED)
  142. tdfx_res_ctx.handle=ctx.handle;
  143. return 0;
  144. }
  145. int tdfx_getctx(struct inode *inode, struct file *filp, unsigned int cmd,
  146. unsigned long arg)
  147. {
  148. drm_ctx_t ctx;
  149. if (copy_from_user(&ctx, (drm_ctx_t*)arg, sizeof(ctx)))
  150. return -EFAULT;
  151. /* This is 0, because we don't handle any context flags */
  152. ctx.flags = 0;
  153. if (copy_to_user((drm_ctx_t*)arg, &ctx, sizeof(ctx)))
  154. return -EFAULT;
  155. return 0;
  156. }
  157. int tdfx_switchctx(struct inode *inode, struct file *filp, unsigned int cmd,
  158.    unsigned long arg)
  159. {
  160. drm_file_t *priv = filp->private_data;
  161. drm_device_t *dev = priv->dev;
  162. drm_ctx_t ctx;
  163. if (copy_from_user(&ctx, (drm_ctx_t *)arg, sizeof(ctx)))
  164. return -EFAULT;
  165. DRM_DEBUG("%dn", ctx.handle);
  166. return tdfx_context_switch(dev, dev->last_context, ctx.handle);
  167. }
  168. int tdfx_newctx(struct inode *inode, struct file *filp, unsigned int cmd,
  169. unsigned long arg)
  170. {
  171. drm_file_t *priv = filp->private_data;
  172. drm_device_t *dev = priv->dev;
  173. drm_ctx_t ctx;
  174. if (copy_from_user(&ctx, (drm_ctx_t *)arg, sizeof(ctx)))
  175. return -EFAULT;
  176. DRM_DEBUG("%dn", ctx.handle);
  177. tdfx_context_switch_complete(dev, ctx.handle);
  178. return 0;
  179. }
  180. int tdfx_rmctx(struct inode *inode, struct file *filp, unsigned int cmd,
  181.        unsigned long arg)
  182. {
  183. drm_file_t      *priv   = filp->private_data;
  184. drm_device_t    *dev    = priv->dev;
  185. drm_ctx_t ctx;
  186. if (copy_from_user(&ctx, (drm_ctx_t *)arg, sizeof(ctx)))
  187. return -EFAULT;
  188. DRM_DEBUG("%dn", ctx.handle);
  189. drm_ctxbitmap_free(dev, ctx.handle);
  190. return 0;
  191. }