1 /* i810_context.c -- IOCTLs for i810 contexts -*- linux-c -*-
2 * Created: Mon Dec 13 09:51:35 1999 by faith@precisioninsight.com
4 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
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:
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
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.
26 * Author: Rickard E. (Rik) Faith <faith@precisioninsight.com>
32 #include <linux/sched.h>
34 #define __NO_VERSION__
38 static int i810_alloc_queue(drm_device_t *dev)
40 int temp = drm_ctxbitmap_next(dev);
41 DRM_DEBUG("i810_alloc_queue: %d\n", temp);
45 int i810_context_switch(drm_device_t *dev, int old, int new)
49 atomic_inc(&dev->total_ctx);
51 if (test_and_set_bit(0, &dev->context_flag)) {
52 DRM_ERROR("Reentering -- FIXME\n");
57 dev->ctx_start = get_cycles();
60 DRM_DEBUG("Context switch from %d to %d\n", old, new);
62 if (new == dev->last_context) {
63 clear_bit(0, &dev->context_flag);
67 if (drm_flags & DRM_FLAG_NOCTX) {
68 i810_context_switch_complete(dev, new);
70 sprintf(buf, "C %d %d\n", old, new);
71 drm_write_string(dev, buf);
77 int i810_context_switch_complete(drm_device_t *dev, int new)
79 dev->last_context = new; /* PRE/POST: This is the _only_ writer. */
80 dev->last_switch = jiffies;
82 if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock)) {
83 DRM_ERROR("Lock isn't held after context switch\n");
86 /* If a context switch is ever initiated
87 when the kernel holds the lock, release
90 atomic_inc(&dev->histo.ctx[drm_histogram_slot(get_cycles()
94 clear_bit(0, &dev->context_flag);
95 wake_up(&dev->context_wait);
100 int i810_resctx(struct inode *inode, struct file *filp, unsigned int cmd,
107 DRM_DEBUG("%d\n", DRM_RESERVED_CONTEXTS);
108 copy_from_user_ret(&res, (drm_ctx_res_t *)arg, sizeof(res), -EFAULT);
109 if (res.count >= DRM_RESERVED_CONTEXTS) {
110 memset(&ctx, 0, sizeof(ctx));
111 for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
113 copy_to_user_ret(&res.contexts[i],
119 res.count = DRM_RESERVED_CONTEXTS;
120 copy_to_user_ret((drm_ctx_res_t *)arg, &res, sizeof(res), -EFAULT);
124 int i810_addctx(struct inode *inode, struct file *filp, unsigned int cmd,
127 drm_file_t *priv = filp->private_data;
128 drm_device_t *dev = priv->dev;
131 copy_from_user_ret(&ctx, (drm_ctx_t *)arg, sizeof(ctx), -EFAULT);
132 if ((ctx.handle = i810_alloc_queue(dev)) == DRM_KERNEL_CONTEXT) {
133 /* Skip kernel's context and get a new one. */
134 ctx.handle = i810_alloc_queue(dev);
136 if (ctx.handle == -1) {
137 DRM_DEBUG("Not enough free contexts.\n");
138 /* Should this return -EBUSY instead? */
141 DRM_DEBUG("%d\n", ctx.handle);
142 copy_to_user_ret((drm_ctx_t *)arg, &ctx, sizeof(ctx), -EFAULT);
146 int i810_modctx(struct inode *inode, struct file *filp, unsigned int cmd,
149 /* This does nothing for the i810 */
153 int i810_getctx(struct inode *inode, struct file *filp, unsigned int cmd,
158 copy_from_user_ret(&ctx, (drm_ctx_t*)arg, sizeof(ctx), -EFAULT);
159 /* This is 0, because we don't hanlde any context flags */
161 copy_to_user_ret((drm_ctx_t*)arg, &ctx, sizeof(ctx), -EFAULT);
165 int i810_switchctx(struct inode *inode, struct file *filp, unsigned int cmd,
168 drm_file_t *priv = filp->private_data;
169 drm_device_t *dev = priv->dev;
172 copy_from_user_ret(&ctx, (drm_ctx_t *)arg, sizeof(ctx), -EFAULT);
173 DRM_DEBUG("%d\n", ctx.handle);
174 return i810_context_switch(dev, dev->last_context, ctx.handle);
177 int i810_newctx(struct inode *inode, struct file *filp, unsigned int cmd,
180 drm_file_t *priv = filp->private_data;
181 drm_device_t *dev = priv->dev;
184 copy_from_user_ret(&ctx, (drm_ctx_t *)arg, sizeof(ctx), -EFAULT);
185 DRM_DEBUG("%d\n", ctx.handle);
186 i810_context_switch_complete(dev, ctx.handle);
191 int i810_rmctx(struct inode *inode, struct file *filp, unsigned int cmd,
194 drm_file_t *priv = filp->private_data;
195 drm_device_t *dev = priv->dev;
198 copy_from_user_ret(&ctx, (drm_ctx_t *)arg, sizeof(ctx), -EFAULT);
199 DRM_DEBUG("%d\n", ctx.handle);
200 if(ctx.handle != DRM_KERNEL_CONTEXT) {
201 drm_ctxbitmap_free(dev, ctx.handle);