Merge with Linux 2.4.0-test6-pre2.
[linux-2.6/linux-mips.git] / drivers / char / drm / i810_context.c
blob85c0877b6bfe827fae02c840e2dcbc563fd54428
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.
5 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
6 * All Rights Reserved.
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
17 * Software.
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 * Authors: Rickard E. (Rik) Faith <faith@valinux.com>
28 * Jeff Hartmann <jhartmann@valinux.com>
32 #define __NO_VERSION__
33 #include "drmP.h"
34 #include "i810_drv.h"
36 static int i810_alloc_queue(drm_device_t *dev)
38 int temp = drm_ctxbitmap_next(dev);
39 DRM_DEBUG("i810_alloc_queue: %d\n", temp);
40 return temp;
43 int i810_context_switch(drm_device_t *dev, int old, int new)
45 char buf[64];
47 atomic_inc(&dev->total_ctx);
49 if (test_and_set_bit(0, &dev->context_flag)) {
50 DRM_ERROR("Reentering -- FIXME\n");
51 return -EBUSY;
54 #if DRM_DMA_HISTOGRAM
55 dev->ctx_start = get_cycles();
56 #endif
58 DRM_DEBUG("Context switch from %d to %d\n", old, new);
60 if (new == dev->last_context) {
61 clear_bit(0, &dev->context_flag);
62 return 0;
65 if (drm_flags & DRM_FLAG_NOCTX) {
66 i810_context_switch_complete(dev, new);
67 } else {
68 sprintf(buf, "C %d %d\n", old, new);
69 drm_write_string(dev, buf);
72 return 0;
75 int i810_context_switch_complete(drm_device_t *dev, int new)
77 dev->last_context = new; /* PRE/POST: This is the _only_ writer. */
78 dev->last_switch = jiffies;
80 if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock)) {
81 DRM_ERROR("Lock isn't held after context switch\n");
84 /* If a context switch is ever initiated
85 when the kernel holds the lock, release
86 that lock here. */
87 #if DRM_DMA_HISTOGRAM
88 atomic_inc(&dev->histo.ctx[drm_histogram_slot(get_cycles()
89 - dev->ctx_start)]);
91 #endif
92 clear_bit(0, &dev->context_flag);
93 wake_up(&dev->context_wait);
95 return 0;
98 int i810_resctx(struct inode *inode, struct file *filp, unsigned int cmd,
99 unsigned long arg)
101 drm_ctx_res_t res;
102 drm_ctx_t ctx;
103 int i;
105 DRM_DEBUG("%d\n", DRM_RESERVED_CONTEXTS);
106 copy_from_user_ret(&res, (drm_ctx_res_t *)arg, sizeof(res), -EFAULT);
107 if (res.count >= DRM_RESERVED_CONTEXTS) {
108 memset(&ctx, 0, sizeof(ctx));
109 for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
110 ctx.handle = i;
111 copy_to_user_ret(&res.contexts[i],
113 sizeof(i),
114 -EFAULT);
117 res.count = DRM_RESERVED_CONTEXTS;
118 copy_to_user_ret((drm_ctx_res_t *)arg, &res, sizeof(res), -EFAULT);
119 return 0;
122 int i810_addctx(struct inode *inode, struct file *filp, unsigned int cmd,
123 unsigned long arg)
125 drm_file_t *priv = filp->private_data;
126 drm_device_t *dev = priv->dev;
127 drm_ctx_t ctx;
129 copy_from_user_ret(&ctx, (drm_ctx_t *)arg, sizeof(ctx), -EFAULT);
130 if ((ctx.handle = i810_alloc_queue(dev)) == DRM_KERNEL_CONTEXT) {
131 /* Skip kernel's context and get a new one. */
132 ctx.handle = i810_alloc_queue(dev);
134 if (ctx.handle == -1) {
135 DRM_DEBUG("Not enough free contexts.\n");
136 /* Should this return -EBUSY instead? */
137 return -ENOMEM;
139 DRM_DEBUG("%d\n", ctx.handle);
140 copy_to_user_ret((drm_ctx_t *)arg, &ctx, sizeof(ctx), -EFAULT);
141 return 0;
144 int i810_modctx(struct inode *inode, struct file *filp, unsigned int cmd,
145 unsigned long arg)
147 /* This does nothing for the i810 */
148 return 0;
151 int i810_getctx(struct inode *inode, struct file *filp, unsigned int cmd,
152 unsigned long arg)
154 drm_ctx_t ctx;
156 copy_from_user_ret(&ctx, (drm_ctx_t*)arg, sizeof(ctx), -EFAULT);
157 /* This is 0, because we don't hanlde any context flags */
158 ctx.flags = 0;
159 copy_to_user_ret((drm_ctx_t*)arg, &ctx, sizeof(ctx), -EFAULT);
160 return 0;
163 int i810_switchctx(struct inode *inode, struct file *filp, unsigned int cmd,
164 unsigned long arg)
166 drm_file_t *priv = filp->private_data;
167 drm_device_t *dev = priv->dev;
168 drm_ctx_t ctx;
170 copy_from_user_ret(&ctx, (drm_ctx_t *)arg, sizeof(ctx), -EFAULT);
171 DRM_DEBUG("%d\n", ctx.handle);
172 return i810_context_switch(dev, dev->last_context, ctx.handle);
175 int i810_newctx(struct inode *inode, struct file *filp, unsigned int cmd,
176 unsigned long arg)
178 drm_file_t *priv = filp->private_data;
179 drm_device_t *dev = priv->dev;
180 drm_ctx_t ctx;
182 copy_from_user_ret(&ctx, (drm_ctx_t *)arg, sizeof(ctx), -EFAULT);
183 DRM_DEBUG("%d\n", ctx.handle);
184 i810_context_switch_complete(dev, ctx.handle);
186 return 0;
189 int i810_rmctx(struct inode *inode, struct file *filp, unsigned int cmd,
190 unsigned long arg)
192 drm_file_t *priv = filp->private_data;
193 drm_device_t *dev = priv->dev;
194 drm_ctx_t ctx;
196 copy_from_user_ret(&ctx, (drm_ctx_t *)arg, sizeof(ctx), -EFAULT);
197 DRM_DEBUG("%d\n", ctx.handle);
198 if(ctx.handle != DRM_KERNEL_CONTEXT) {
199 drm_ctxbitmap_free(dev, ctx.handle);
202 return 0;