1 /* mga_context.c -- IOCTLs for mga 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.
5 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
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.
27 * Author: Rickard E. (Rik) Faith <faith@valinux.com>
28 * Jeff Hartmann <jhartmann@valinux.com>
32 #define __NO_VERSION__
36 static int mga_alloc_queue(drm_device_t *dev)
38 return drm_ctxbitmap_next(dev);
41 int mga_context_switch(drm_device_t *dev, int old, int new)
45 atomic_inc(&dev->total_ctx);
47 if (test_and_set_bit(0, &dev->context_flag)) {
48 DRM_ERROR("Reentering -- FIXME\n");
53 dev->ctx_start = get_cycles();
56 DRM_DEBUG("Context switch from %d to %d\n", old, new);
58 if (new == dev->last_context) {
59 clear_bit(0, &dev->context_flag);
63 if (drm_flags & DRM_FLAG_NOCTX) {
64 mga_context_switch_complete(dev, new);
66 sprintf(buf, "C %d %d\n", old, new);
67 drm_write_string(dev, buf);
73 int mga_context_switch_complete(drm_device_t *dev, int new)
75 dev->last_context = new; /* PRE/POST: This is the _only_ writer. */
76 dev->last_switch = jiffies;
78 if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock)) {
79 DRM_ERROR("Lock isn't held after context switch\n");
82 /* If a context switch is ever initiated
83 when the kernel holds the lock, release
86 atomic_inc(&dev->histo.ctx[drm_histogram_slot(get_cycles()
90 clear_bit(0, &dev->context_flag);
91 wake_up(&dev->context_wait);
96 int mga_resctx(struct inode *inode, struct file *filp, unsigned int cmd,
103 if (copy_from_user(&res, (drm_ctx_res_t *)arg, sizeof(res)))
105 if (res.count >= DRM_RESERVED_CONTEXTS) {
106 memset(&ctx, 0, sizeof(ctx));
107 for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
109 if (copy_to_user(&res.contexts[i],
115 res.count = DRM_RESERVED_CONTEXTS;
116 if (copy_to_user((drm_ctx_res_t *)arg, &res, sizeof(res)))
121 int mga_addctx(struct inode *inode, struct file *filp, unsigned int cmd,
124 drm_file_t *priv = filp->private_data;
125 drm_device_t *dev = priv->dev;
128 if (copy_from_user(&ctx, (drm_ctx_t *)arg, sizeof(ctx)))
130 if ((ctx.handle = mga_alloc_queue(dev)) == DRM_KERNEL_CONTEXT) {
131 /* Skip kernel's context and get a new one. */
132 ctx.handle = mga_alloc_queue(dev);
134 if (ctx.handle == -1) {
137 DRM_DEBUG("%d\n", ctx.handle);
138 if (copy_to_user((drm_ctx_t *)arg, &ctx, sizeof(ctx)))
143 int mga_modctx(struct inode *inode, struct file *filp, unsigned int cmd,
146 /* This does nothing for the mga */
150 int mga_getctx(struct inode *inode, struct file *filp, unsigned int cmd,
155 if (copy_from_user(&ctx, (drm_ctx_t*)arg, sizeof(ctx)))
157 /* This is 0, because we don't hanlde any context flags */
159 if (copy_to_user((drm_ctx_t*)arg, &ctx, sizeof(ctx)))
164 int mga_switchctx(struct inode *inode, struct file *filp, unsigned int cmd,
167 drm_file_t *priv = filp->private_data;
168 drm_device_t *dev = priv->dev;
171 if (copy_from_user(&ctx, (drm_ctx_t *)arg, sizeof(ctx)))
173 DRM_DEBUG("%d\n", ctx.handle);
174 return mga_context_switch(dev, dev->last_context, ctx.handle);
177 int mga_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 if (copy_from_user(&ctx, (drm_ctx_t *)arg, sizeof(ctx)))
186 DRM_DEBUG("%d\n", ctx.handle);
187 mga_context_switch_complete(dev, ctx.handle);
192 int mga_rmctx(struct inode *inode, struct file *filp, unsigned int cmd,
195 drm_file_t *priv = filp->private_data;
196 drm_device_t *dev = priv->dev;
199 if (copy_from_user(&ctx, (drm_ctx_t *)arg, sizeof(ctx)))
201 DRM_DEBUG("%d\n", ctx.handle);
202 if(ctx.handle == DRM_KERNEL_CONTEXT+1) priv->remove_auth_on_close = 1;
204 if(ctx.handle != DRM_KERNEL_CONTEXT) {
205 drm_ctxbitmap_free(dev, ctx.handle);