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

嵌入式Linux

开发平台:

Unix_Linux

  1. /* radeon_context.c -- IOCTLs for Radeon contexts -*- linux-c -*-
  2.  *
  3.  * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
  4.  * Copyright 2000 VA Linux Systems, Inc., Fremont, California.
  5.  * All Rights Reserved.
  6.  *
  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:
  13.  *
  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
  16.  * Software.
  17.  *
  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.
  25.  *
  26.  * Author: Kevin E. Martin <martin@valinux.com>
  27.  *         Rickard E. (Rik) Faith <faith@valinux.com>
  28.  *
  29.  */
  30. #define __NO_VERSION__
  31. #include "drmP.h"
  32. #include "radeon_drv.h"
  33. extern drm_ctx_t radeon_res_ctx;
  34. static int radeon_alloc_queue(drm_device_t *dev)
  35. {
  36. return drm_ctxbitmap_next(dev);
  37. }
  38. int radeon_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.                 radeon_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 radeon_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 radeon_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], &i, sizeof(i)))
  94. return -EFAULT;
  95. }
  96. }
  97. res.count = DRM_RESERVED_CONTEXTS;
  98. if (copy_to_user((drm_ctx_res_t *)arg, &res, sizeof(res)))
  99. return -EFAULT;
  100. return 0;
  101. }
  102. int radeon_addctx(struct inode *inode, struct file *filp, unsigned int cmd,
  103.   unsigned long arg)
  104. {
  105. drm_file_t *priv = filp->private_data;
  106. drm_device_t *dev = priv->dev;
  107. drm_ctx_t ctx;
  108. if (copy_from_user(&ctx, (drm_ctx_t *)arg, sizeof(ctx)))
  109. return -EFAULT;
  110. if ((ctx.handle = radeon_alloc_queue(dev)) == DRM_KERNEL_CONTEXT) {
  111. /* Skip kernel's context and get a new one. */
  112. ctx.handle = radeon_alloc_queue(dev);
  113. }
  114. DRM_DEBUG("%dn", ctx.handle);
  115. if (ctx.handle == -1) {
  116. DRM_DEBUG("Not enough free contexts.n");
  117. /* Should this return -EBUSY instead? */
  118. return -ENOMEM;
  119. }
  120. if (copy_to_user((drm_ctx_t *)arg, &ctx, sizeof(ctx)))
  121. return -EFAULT;
  122. return 0;
  123. }
  124. int radeon_modctx(struct inode *inode, struct file *filp, unsigned int cmd,
  125.   unsigned long arg)
  126. {
  127. drm_ctx_t ctx;
  128. if (copy_from_user(&ctx, (drm_ctx_t*)arg, sizeof(ctx)))
  129. return -EFAULT;
  130. if (ctx.flags==_DRM_CONTEXT_PRESERVED)
  131. radeon_res_ctx.handle=ctx.handle;
  132. return 0;
  133. }
  134. int radeon_getctx(struct inode *inode, struct file *filp, unsigned int cmd,
  135.   unsigned long arg)
  136. {
  137. drm_ctx_t ctx;
  138. if (copy_from_user(&ctx, (drm_ctx_t*)arg, sizeof(ctx)))
  139. return -EFAULT;
  140. /* This is 0, because we don't hanlde any context flags */
  141. ctx.flags = 0;
  142. if (copy_to_user((drm_ctx_t*)arg, &ctx, sizeof(ctx)))
  143. return -EFAULT;
  144. return 0;
  145. }
  146. int radeon_switchctx(struct inode *inode, struct file *filp, unsigned int cmd,
  147.      unsigned long arg)
  148. {
  149. drm_file_t *priv = filp->private_data;
  150. drm_device_t *dev = priv->dev;
  151. drm_ctx_t ctx;
  152. if (copy_from_user(&ctx, (drm_ctx_t *)arg, sizeof(ctx)))
  153. return -EFAULT;
  154. DRM_DEBUG("%dn", ctx.handle);
  155. return radeon_context_switch(dev, dev->last_context, ctx.handle);
  156. }
  157. int radeon_newctx(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. radeon_context_switch_complete(dev, ctx.handle);
  167. return 0;
  168. }
  169. int radeon_rmctx(struct inode *inode, struct file *filp, unsigned int cmd,
  170.  unsigned long arg)
  171. {
  172. drm_file_t *priv = filp->private_data;
  173. drm_device_t *dev = priv->dev;
  174. drm_ctx_t ctx;
  175. if (copy_from_user(&ctx, (drm_ctx_t *)arg, sizeof(ctx)))
  176. return -EFAULT;
  177. DRM_DEBUG("%dn", ctx.handle);
  178. drm_ctxbitmap_free(dev, ctx.handle);
  179. return 0;
  180. }