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

嵌入式Linux

开发平台:

Unix_Linux

  1. /* i810_context.c -- IOCTLs for i810 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.  * Authors: 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 "i810_drv.h"
  34. static int i810_alloc_queue(drm_device_t *dev)
  35. {
  36.     int temp = drm_ctxbitmap_next(dev);
  37.     DRM_DEBUG("i810_alloc_queue: %dn", temp);
  38. return temp;
  39. }
  40. int i810_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.                 i810_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 i810_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 i810_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 i810_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 = i810_alloc_queue(dev)) == DRM_KERNEL_CONTEXT) {
  121. /* Skip kernel's context and get a new one. */
  122. ctx.handle = i810_alloc_queue(dev);
  123. }
  124.         if (ctx.handle == -1) {
  125. DRM_DEBUG("Not enough free contexts.n");
  126. /* Should this return -EBUSY instead? */
  127. return -ENOMEM;
  128. }
  129. DRM_DEBUG("%dn", ctx.handle);
  130. if (copy_to_user((drm_ctx_t *)arg, &ctx, sizeof(ctx)))
  131. return -EFAULT;
  132. return 0;
  133. }
  134. int i810_modctx(struct inode *inode, struct file *filp, unsigned int cmd,
  135. unsigned long arg)
  136. {
  137.     /* This does nothing for the i810 */
  138. return 0;
  139. }
  140. int i810_getctx(struct inode *inode, struct file *filp, unsigned int cmd,
  141. unsigned long arg)
  142. {
  143. drm_ctx_t ctx;
  144. if (copy_from_user(&ctx, (drm_ctx_t*)arg, sizeof(ctx)))
  145. return -EFAULT;
  146. /* This is 0, because we don't hanlde any context flags */
  147. ctx.flags = 0;
  148. if (copy_to_user((drm_ctx_t*)arg, &ctx, sizeof(ctx)))
  149. return -EFAULT;
  150. return 0;
  151. }
  152. int i810_switchctx(struct inode *inode, struct file *filp, unsigned int cmd,
  153.    unsigned long arg)
  154. {
  155. drm_file_t *priv = filp->private_data;
  156. drm_device_t *dev = priv->dev;
  157. drm_ctx_t ctx;
  158. if (copy_from_user(&ctx, (drm_ctx_t *)arg, sizeof(ctx)))
  159. return -EFAULT;
  160. DRM_DEBUG("%dn", ctx.handle);
  161. return i810_context_switch(dev, dev->last_context, ctx.handle);
  162. }
  163. int i810_newctx(struct inode *inode, struct file *filp, unsigned int cmd,
  164. unsigned long arg)
  165. {
  166. drm_file_t *priv = filp->private_data;
  167. drm_device_t *dev = priv->dev;
  168. drm_ctx_t ctx;
  169. if (copy_from_user(&ctx, (drm_ctx_t *)arg, sizeof(ctx)))
  170. return -EFAULT;
  171. DRM_DEBUG("%dn", ctx.handle);
  172. i810_context_switch_complete(dev, ctx.handle);
  173. return 0;
  174. }
  175. int i810_rmctx(struct inode *inode, struct file *filp, unsigned int cmd,
  176.       unsigned long arg)
  177. {
  178. drm_file_t *priv = filp->private_data;
  179. drm_device_t *dev = priv->dev;
  180. drm_ctx_t ctx;
  181. if (copy_from_user(&ctx, (drm_ctx_t *)arg, sizeof(ctx)))
  182. return -EFAULT;
  183. DRM_DEBUG("%dn", ctx.handle);
  184.     if(ctx.handle != DRM_KERNEL_CONTEXT) {
  185.     drm_ctxbitmap_free(dev, ctx.handle);
  186. }
  187. return 0;
  188. }