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

Linux/Unix编程

开发平台:

Unix_Linux

  1. /* r128_context.c -- IOCTLs for r128 contexts -*- linux-c -*-
  2.  * Created: Mon Dec 13 09:51:35 1999 by faith@precisioninsight.com
  3.  *
  4.  * Copyright 1999, 2000 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.  *
  29.  */
  30. #define __NO_VERSION__
  31. #include "drmP.h"
  32. #include "r128_drv.h"
  33. extern drm_ctx_t r128_res_ctx;
  34. static int r128_alloc_queue(drm_device_t *dev)
  35. {
  36. return drm_ctxbitmap_next(dev);
  37. }
  38. int r128_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.         DRM_DEBUG("Context switch from %d to %dn", old, new);
  50.         if (new == dev->last_context) {
  51.                 clear_bit(0, &dev->context_flag);
  52.                 return 0;
  53.         }
  54.         if (drm_flags & DRM_FLAG_NOCTX) {
  55.                 r128_context_switch_complete(dev, new);
  56.         } else {
  57.                 sprintf(buf, "C %d %dn", old, new);
  58.                 drm_write_string(dev, buf);
  59.         }
  60.         return 0;
  61. }
  62. int r128_context_switch_complete(drm_device_t *dev, int new)
  63. {
  64.         dev->last_context = new;  /* PRE/POST: This is the _only_ writer. */
  65.         dev->last_switch  = jiffies;
  66.         if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock)) {
  67.                 DRM_ERROR("Lock isn't held after context switchn");
  68.         }
  69. /* If a context switch is ever initiated
  70.                                    when the kernel holds the lock, release
  71.                                    that lock here. */
  72. #if DRM_DMA_HISTOGRAM
  73.         atomic_inc(&dev->histo.ctx[drm_histogram_slot(get_cycles()
  74.                                                       - dev->ctx_start)]);
  75. #endif
  76.         clear_bit(0, &dev->context_flag);
  77.         wake_up(&dev->context_wait);
  78.         return 0;
  79. }
  80. int r128_resctx(struct inode *inode, struct file *filp, unsigned int cmd,
  81.        unsigned long arg)
  82. {
  83. drm_ctx_res_t res;
  84. drm_ctx_t ctx;
  85. int i;
  86. DRM_DEBUG("%dn", DRM_RESERVED_CONTEXTS);
  87. if (copy_from_user(&res, (drm_ctx_res_t *)arg, sizeof(res)))
  88. return -EFAULT;
  89. if (res.count >= DRM_RESERVED_CONTEXTS) {
  90. memset(&ctx, 0, sizeof(ctx));
  91. for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
  92. ctx.handle = i;
  93. if (copy_to_user(&res.contexts[i],
  94.  &i,
  95.  sizeof(i)))
  96. return -EFAULT;
  97. }
  98. }
  99. res.count = DRM_RESERVED_CONTEXTS;
  100. if (copy_to_user((drm_ctx_res_t *)arg, &res, sizeof(res)))
  101. return -EFAULT;
  102. return 0;
  103. }
  104. int r128_addctx(struct inode *inode, struct file *filp, unsigned int cmd,
  105.        unsigned long arg)
  106. {
  107. drm_file_t *priv = filp->private_data;
  108. drm_device_t *dev = priv->dev;
  109. drm_ctx_t ctx;
  110. if (copy_from_user(&ctx, (drm_ctx_t *)arg, sizeof(ctx)))
  111. return -EFAULT;
  112. if ((ctx.handle = r128_alloc_queue(dev)) == DRM_KERNEL_CONTEXT) {
  113. /* Skip kernel's context and get a new one. */
  114. ctx.handle = r128_alloc_queue(dev);
  115. }
  116. DRM_DEBUG("%dn", ctx.handle);
  117. if (ctx.handle == -1) {
  118. DRM_DEBUG("Not enough free contexts.n");
  119. /* Should this return -EBUSY instead? */
  120. return -ENOMEM;
  121. }
  122. if (copy_to_user((drm_ctx_t *)arg, &ctx, sizeof(ctx)))
  123. return -EFAULT;
  124. return 0;
  125. }
  126. int r128_modctx(struct inode *inode, struct file *filp, unsigned int cmd,
  127. unsigned long arg)
  128. {
  129. drm_ctx_t ctx;
  130. if (copy_from_user(&ctx, (drm_ctx_t*)arg, sizeof(ctx)))
  131. return -EFAULT;
  132. if (ctx.flags==_DRM_CONTEXT_PRESERVED)
  133. r128_res_ctx.handle=ctx.handle;
  134. return 0;
  135. }
  136. int r128_getctx(struct inode *inode, struct file *filp, unsigned int cmd,
  137. unsigned long arg)
  138. {
  139. drm_ctx_t ctx;
  140. if (copy_from_user(&ctx, (drm_ctx_t*)arg, sizeof(ctx)))
  141. return -EFAULT;
  142. /* This is 0, because we don't hanlde any context flags */
  143. ctx.flags = 0;
  144. if (copy_to_user((drm_ctx_t*)arg, &ctx, sizeof(ctx)))
  145. return -EFAULT;
  146. return 0;
  147. }
  148. int r128_switchctx(struct inode *inode, struct file *filp, unsigned int cmd,
  149.    unsigned long arg)
  150. {
  151. drm_file_t *priv = filp->private_data;
  152. drm_device_t *dev = priv->dev;
  153. drm_ctx_t ctx;
  154. if (copy_from_user(&ctx, (drm_ctx_t *)arg, sizeof(ctx)))
  155. return -EFAULT;
  156. DRM_DEBUG("%dn", ctx.handle);
  157. return r128_context_switch(dev, dev->last_context, ctx.handle);
  158. }
  159. int r128_newctx(struct inode *inode, struct file *filp, unsigned int cmd,
  160. unsigned long arg)
  161. {
  162. drm_file_t *priv = filp->private_data;
  163. drm_device_t *dev = priv->dev;
  164. drm_ctx_t ctx;
  165. if (copy_from_user(&ctx, (drm_ctx_t *)arg, sizeof(ctx)))
  166. return -EFAULT;
  167. DRM_DEBUG("%dn", ctx.handle);
  168. r128_context_switch_complete(dev, ctx.handle);
  169. return 0;
  170. }
  171. int r128_rmctx(struct inode *inode, struct file *filp, unsigned int cmd,
  172.        unsigned long arg)
  173. {
  174. drm_file_t *priv = filp->private_data;
  175. drm_device_t *dev = priv->dev;
  176. drm_ctx_t ctx;
  177. if (copy_from_user(&ctx, (drm_ctx_t *)arg, sizeof(ctx)))
  178. return -EFAULT;
  179. DRM_DEBUG("%dn", ctx.handle);
  180. drm_ctxbitmap_free(dev, ctx.handle);
  181. return 0;
  182. }