1 /* tdfx_context.c -- IOCTLs for tdfx contexts -*- linux-c -*-
2 * Created: Thu Oct 7 10:50:22 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:
28 * Rickard E. (Rik) Faith <faith@valinux.com>
29 * Daryll Strauss <daryll@valinux.com>
30 *
31 */
32
33 #define __NO_VERSION__
34 #include "drmP.h"
35 #include "tdfx_drv.h"
36
37 extern drm_ctx_t tdfx_res_ctx;
38
tdfx_alloc_queue(drm_device_t * dev)39 static int tdfx_alloc_queue(drm_device_t *dev)
40 {
41 return drm_ctxbitmap_next(dev);
42 }
43
tdfx_context_switch(drm_device_t * dev,int old,int new)44 int tdfx_context_switch(drm_device_t *dev, int old, int new)
45 {
46 char buf[64];
47
48 atomic_inc(&dev->total_ctx);
49
50 if (test_and_set_bit(0, &dev->context_flag)) {
51 DRM_ERROR("Reentering -- FIXME\n");
52 return -EBUSY;
53 }
54
55 #if DRM_DMA_HISTOGRAM
56 dev->ctx_start = get_cycles();
57 #endif
58
59 DRM_DEBUG("Context switch from %d to %d\n", old, new);
60
61 if (new == dev->last_context) {
62 clear_bit(0, &dev->context_flag);
63 return 0;
64 }
65
66 if (drm_flags & DRM_FLAG_NOCTX) {
67 tdfx_context_switch_complete(dev, new);
68 } else {
69 sprintf(buf, "C %d %d\n", old, new);
70 drm_write_string(dev, buf);
71 }
72
73 return 0;
74 }
75
tdfx_context_switch_complete(drm_device_t * dev,int new)76 int tdfx_context_switch_complete(drm_device_t *dev, int new)
77 {
78 dev->last_context = new; /* PRE/POST: This is the _only_ writer. */
79 dev->last_switch = jiffies;
80
81 if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock)) {
82 DRM_ERROR("Lock isn't held after context switch\n");
83 }
84
85 /* If a context switch is ever initiated
86 when the kernel holds the lock, release
87 that lock here. */
88 #if DRM_DMA_HISTOGRAM
89 atomic_inc(&dev->histo.ctx[drm_histogram_slot(get_cycles()
90 - dev->ctx_start)]);
91
92 #endif
93 clear_bit(0, &dev->context_flag);
94 wake_up(&dev->context_wait);
95
96 return 0;
97 }
98
99
tdfx_resctx(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)100 int tdfx_resctx(struct inode *inode, struct file *filp, unsigned int cmd,
101 unsigned long arg)
102 {
103 drm_ctx_res_t res;
104 drm_ctx_t ctx;
105 int i;
106
107 DRM_DEBUG("%d\n", DRM_RESERVED_CONTEXTS);
108 if (copy_from_user(&res, (drm_ctx_res_t *)arg, sizeof(res)))
109 return -EFAULT;
110 if (res.count >= DRM_RESERVED_CONTEXTS) {
111 memset(&ctx, 0, sizeof(ctx));
112 for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
113 ctx.handle = i;
114 if (copy_to_user(&res.contexts[i],
115 &i,
116 sizeof(i)))
117 return -EFAULT;
118 }
119 }
120 res.count = DRM_RESERVED_CONTEXTS;
121 if (copy_to_user((drm_ctx_res_t *)arg, &res, sizeof(res)))
122 return -EFAULT;
123 return 0;
124 }
125
126
tdfx_addctx(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)127 int tdfx_addctx(struct inode *inode, struct file *filp, unsigned int cmd,
128 unsigned long arg)
129 {
130 drm_file_t *priv = filp->private_data;
131 drm_device_t *dev = priv->dev;
132 drm_ctx_t ctx;
133
134 if (copy_from_user(&ctx, (drm_ctx_t *)arg, sizeof(ctx)))
135 return -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);
139 }
140 DRM_DEBUG("%d\n", ctx.handle);
141 if (ctx.handle == -1) {
142 DRM_DEBUG("Not enough free contexts.\n");
143 /* Should this return -EBUSY instead? */
144 return -ENOMEM;
145 }
146
147 if (copy_to_user((drm_ctx_t *)arg, &ctx, sizeof(ctx)))
148 return -EFAULT;
149 return 0;
150 }
151
tdfx_modctx(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)152 int tdfx_modctx(struct inode *inode, struct file *filp, unsigned int cmd,
153 unsigned long arg)
154 {
155 drm_ctx_t ctx;
156
157 if (copy_from_user(&ctx, (drm_ctx_t*)arg, sizeof(ctx)))
158 return -EFAULT;
159 if (ctx.flags==_DRM_CONTEXT_PRESERVED)
160 tdfx_res_ctx.handle=ctx.handle;
161 return 0;
162 }
163
tdfx_getctx(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)164 int tdfx_getctx(struct inode *inode, struct file *filp, unsigned int cmd,
165 unsigned long arg)
166 {
167 drm_ctx_t ctx;
168
169 if (copy_from_user(&ctx, (drm_ctx_t*)arg, sizeof(ctx)))
170 return -EFAULT;
171 /* This is 0, because we don't handle any context flags */
172 ctx.flags = 0;
173 if (copy_to_user((drm_ctx_t*)arg, &ctx, sizeof(ctx)))
174 return -EFAULT;
175 return 0;
176 }
177
tdfx_switchctx(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)178 int tdfx_switchctx(struct inode *inode, struct file *filp, unsigned int cmd,
179 unsigned long arg)
180 {
181 drm_file_t *priv = filp->private_data;
182 drm_device_t *dev = priv->dev;
183 drm_ctx_t ctx;
184
185 if (copy_from_user(&ctx, (drm_ctx_t *)arg, sizeof(ctx)))
186 return -EFAULT;
187 DRM_DEBUG("%d\n", ctx.handle);
188 return tdfx_context_switch(dev, dev->last_context, ctx.handle);
189 }
190
tdfx_newctx(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)191 int tdfx_newctx(struct inode *inode, struct file *filp, unsigned int cmd,
192 unsigned long arg)
193 {
194 drm_file_t *priv = filp->private_data;
195 drm_device_t *dev = priv->dev;
196 drm_ctx_t ctx;
197
198 if (copy_from_user(&ctx, (drm_ctx_t *)arg, sizeof(ctx)))
199 return -EFAULT;
200 DRM_DEBUG("%d\n", ctx.handle);
201 tdfx_context_switch_complete(dev, ctx.handle);
202
203 return 0;
204 }
205
tdfx_rmctx(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)206 int tdfx_rmctx(struct inode *inode, struct file *filp, unsigned int cmd,
207 unsigned long arg)
208 {
209 drm_file_t *priv = filp->private_data;
210 drm_device_t *dev = priv->dev;
211 drm_ctx_t ctx;
212
213 if (copy_from_user(&ctx, (drm_ctx_t *)arg, sizeof(ctx)))
214 return -EFAULT;
215 DRM_DEBUG("%d\n", ctx.handle);
216 drm_ctxbitmap_free(dev, ctx.handle);
217
218 return 0;
219 }
220