1 /* tdfx_context.c -- IOCTLs for tdfx contexts -*- linux-c -*-
2 * Created: Thu Oct 7 10:50:22 1999 by faith@precisioninsight.com
3 * Revised: Sat Oct 9 23:39:56 1999 by faith@precisioninsight.com
5 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
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:
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
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.
32 #include <linux/sched.h>
34 #define __NO_VERSION__
38 extern drm_ctx_t tdfx_res_ctx;
40 static int tdfx_alloc_queue(drm_device_t *dev)
42 static int context = 0;
44 return ++context; /* Should this reuse contexts in the future? */
47 int tdfx_context_switch(drm_device_t *dev, int old, int new)
51 atomic_inc(&dev->total_ctx);
53 if (test_and_set_bit(0, &dev->context_flag)) {
54 DRM_ERROR("Reentering -- FIXME\n");
59 dev->ctx_start = get_cycles();
62 DRM_DEBUG("Context switch from %d to %d\n", old, new);
64 if (new == dev->last_context) {
65 clear_bit(0, &dev->context_flag);
69 if (drm_flags & DRM_FLAG_NOCTX) {
70 tdfx_context_switch_complete(dev, new);
72 sprintf(buf, "C %d %d\n", old, new);
73 drm_write_string(dev, buf);
79 int tdfx_context_switch_complete(drm_device_t *dev, int new)
81 dev->last_context = new; /* PRE/POST: This is the _only_ writer. */
82 dev->last_switch = jiffies;
84 if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock)) {
85 DRM_ERROR("Lock isn't held after context switch\n");
88 /* If a context switch is ever initiated
89 when the kernel holds the lock, release
92 atomic_inc(&dev->histo.ctx[drm_histogram_slot(get_cycles()
96 clear_bit(0, &dev->context_flag);
97 wake_up(&dev->context_wait);
103 int tdfx_resctx(struct inode *inode, struct file *filp, unsigned int cmd,
110 DRM_DEBUG("%d\n", DRM_RESERVED_CONTEXTS);
111 copy_from_user_ret(&res, (drm_ctx_res_t *)arg, sizeof(res), -EFAULT);
112 if (res.count >= DRM_RESERVED_CONTEXTS) {
113 memset(&ctx, 0, sizeof(ctx));
114 for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
116 copy_to_user_ret(&res.contexts[i],
122 res.count = DRM_RESERVED_CONTEXTS;
123 copy_to_user_ret((drm_ctx_res_t *)arg, &res, sizeof(res), -EFAULT);
128 int tdfx_addctx(struct inode *inode, struct file *filp, unsigned int cmd,
131 drm_file_t *priv = filp->private_data;
132 drm_device_t *dev = priv->dev;
135 copy_from_user_ret(&ctx, (drm_ctx_t *)arg, sizeof(ctx), -EFAULT);
136 if ((ctx.handle = tdfx_alloc_queue(dev)) == DRM_KERNEL_CONTEXT) {
137 /* Skip kernel's context and get a new one. */
138 ctx.handle = tdfx_alloc_queue(dev);
140 DRM_DEBUG("%d\n", ctx.handle);
141 copy_to_user_ret((drm_ctx_t *)arg, &ctx, sizeof(ctx), -EFAULT);
145 int tdfx_modctx(struct inode *inode, struct file *filp, unsigned int cmd,
150 copy_from_user_ret(&ctx, (drm_ctx_t*)arg, sizeof(ctx), -EFAULT);
151 if (ctx.flags==_DRM_CONTEXT_PRESERVED)
152 tdfx_res_ctx.handle=ctx.handle;
156 int tdfx_getctx(struct inode *inode, struct file *filp, unsigned int cmd,
161 copy_from_user_ret(&ctx, (drm_ctx_t*)arg, sizeof(ctx), -EFAULT);
162 /* This is 0, because we don't hanlde any context flags */
164 copy_to_user_ret((drm_ctx_t*)arg, &ctx, sizeof(ctx), -EFAULT);
168 int tdfx_switchctx(struct inode *inode, struct file *filp, unsigned int cmd,
171 drm_file_t *priv = filp->private_data;
172 drm_device_t *dev = priv->dev;
175 copy_from_user_ret(&ctx, (drm_ctx_t *)arg, sizeof(ctx), -EFAULT);
176 DRM_DEBUG("%d\n", ctx.handle);
177 return tdfx_context_switch(dev, dev->last_context, ctx.handle);
180 int tdfx_newctx(struct inode *inode, struct file *filp, unsigned int cmd,
183 drm_file_t *priv = filp->private_data;
184 drm_device_t *dev = priv->dev;
187 copy_from_user_ret(&ctx, (drm_ctx_t *)arg, sizeof(ctx), -EFAULT);
188 DRM_DEBUG("%d\n", ctx.handle);
189 tdfx_context_switch_complete(dev, ctx.handle);
194 int tdfx_rmctx(struct inode *inode, struct file *filp, unsigned int cmd,
199 copy_from_user_ret(&ctx, (drm_ctx_t *)arg, sizeof(ctx), -EFAULT);
200 DRM_DEBUG("%d\n", ctx.handle);
201 /* This is currently a noop because we
202 don't reuse context values. Perhaps we