1 /* i810_context.c -- IOCTLs for i810 contexts -*- linux-c -*-
2 * Created: Mon Dec 13 09:51:35 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: Rickard E. (Rik) Faith <faith@valinux.com>
28 * Jeff Hartmann <jhartmann@valinux.com>
29 *
30 */
31
32 #define __NO_VERSION__
33 #include "drmP.h"
34 #include "i810_drv.h"
35
i810_alloc_queue(drm_device_t * dev)36 static int i810_alloc_queue(drm_device_t *dev)
37 {
38 int temp = drm_ctxbitmap_next(dev);
39 DRM_DEBUG("i810_alloc_queue: %d\n", temp);
40 return temp;
41 }
42
i810_context_switch(drm_device_t * dev,int old,int new)43 int i810_context_switch(drm_device_t *dev, int old, int new)
44 {
45 char buf[64];
46
47 atomic_inc(&dev->total_ctx);
48
49 if (test_and_set_bit(0, &dev->context_flag)) {
50 DRM_ERROR("Reentering -- FIXME\n");
51 return -EBUSY;
52 }
53
54 #if DRM_DMA_HISTOGRAM
55 dev->ctx_start = get_cycles();
56 #endif
57
58 DRM_DEBUG("Context switch from %d to %d\n", old, new);
59
60 if (new == dev->last_context) {
61 clear_bit(0, &dev->context_flag);
62 return 0;
63 }
64
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);
70 }
71
72 return 0;
73 }
74
i810_context_switch_complete(drm_device_t * dev,int new)75 int i810_context_switch_complete(drm_device_t *dev, int new)
76 {
77 dev->last_context = new; /* PRE/POST: This is the _only_ writer. */
78 dev->last_switch = jiffies;
79
80 if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock)) {
81 DRM_ERROR("Lock isn't held after context switch\n");
82 }
83
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)]);
90
91 #endif
92 clear_bit(0, &dev->context_flag);
93 wake_up(&dev->context_wait);
94
95 return 0;
96 }
97
i810_resctx(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)98 int i810_resctx(struct inode *inode, struct file *filp, unsigned int cmd,
99 unsigned long arg)
100 {
101 drm_ctx_res_t res;
102 drm_ctx_t ctx;
103 int i;
104
105 DRM_DEBUG("%d\n", DRM_RESERVED_CONTEXTS);
106 if (copy_from_user(&res, (drm_ctx_res_t *)arg, sizeof(res)))
107 return -EFAULT;
108 if (res.count >= DRM_RESERVED_CONTEXTS) {
109 memset(&ctx, 0, sizeof(ctx));
110 for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
111 ctx.handle = i;
112 if (copy_to_user(&res.contexts[i],
113 &i,
114 sizeof(i)))
115 return -EFAULT;
116 }
117 }
118 res.count = DRM_RESERVED_CONTEXTS;
119 if (copy_to_user((drm_ctx_res_t *)arg, &res, sizeof(res)))
120 return -EFAULT;
121 return 0;
122 }
123
i810_addctx(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)124 int i810_addctx(struct inode *inode, struct file *filp, unsigned int cmd,
125 unsigned long arg)
126 {
127 drm_file_t *priv = filp->private_data;
128 drm_device_t *dev = priv->dev;
129 drm_ctx_t ctx;
130
131 if (copy_from_user(&ctx, (drm_ctx_t *)arg, sizeof(ctx)))
132 return -EFAULT;
133 if ((ctx.handle = i810_alloc_queue(dev)) == DRM_KERNEL_CONTEXT) {
134 /* Skip kernel's context and get a new one. */
135 ctx.handle = i810_alloc_queue(dev);
136 }
137 if (ctx.handle == -1) {
138 DRM_DEBUG("Not enough free contexts.\n");
139 /* Should this return -EBUSY instead? */
140 return -ENOMEM;
141 }
142 DRM_DEBUG("%d\n", ctx.handle);
143 if (copy_to_user((drm_ctx_t *)arg, &ctx, sizeof(ctx)))
144 return -EFAULT;
145 return 0;
146 }
147
i810_modctx(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)148 int i810_modctx(struct inode *inode, struct file *filp, unsigned int cmd,
149 unsigned long arg)
150 {
151 /* This does nothing for the i810 */
152 return 0;
153 }
154
i810_getctx(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)155 int i810_getctx(struct inode *inode, struct file *filp, unsigned int cmd,
156 unsigned long arg)
157 {
158 drm_ctx_t ctx;
159
160 if (copy_from_user(&ctx, (drm_ctx_t*)arg, sizeof(ctx)))
161 return -EFAULT;
162 /* This is 0, because we don't hanlde any context flags */
163 ctx.flags = 0;
164 if (copy_to_user((drm_ctx_t*)arg, &ctx, sizeof(ctx)))
165 return -EFAULT;
166 return 0;
167 }
168
i810_switchctx(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)169 int i810_switchctx(struct inode *inode, struct file *filp, unsigned int cmd,
170 unsigned long arg)
171 {
172 drm_file_t *priv = filp->private_data;
173 drm_device_t *dev = priv->dev;
174 drm_ctx_t ctx;
175
176 if (copy_from_user(&ctx, (drm_ctx_t *)arg, sizeof(ctx)))
177 return -EFAULT;
178 DRM_DEBUG("%d\n", ctx.handle);
179 return i810_context_switch(dev, dev->last_context, ctx.handle);
180 }
181
i810_newctx(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)182 int i810_newctx(struct inode *inode, struct file *filp, unsigned int cmd,
183 unsigned long arg)
184 {
185 drm_file_t *priv = filp->private_data;
186 drm_device_t *dev = priv->dev;
187 drm_ctx_t ctx;
188
189 if (copy_from_user(&ctx, (drm_ctx_t *)arg, sizeof(ctx)))
190 return -EFAULT;
191 DRM_DEBUG("%d\n", ctx.handle);
192 i810_context_switch_complete(dev, ctx.handle);
193
194 return 0;
195 }
196
i810_rmctx(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)197 int i810_rmctx(struct inode *inode, struct file *filp, unsigned int cmd,
198 unsigned long arg)
199 {
200 drm_file_t *priv = filp->private_data;
201 drm_device_t *dev = priv->dev;
202 drm_ctx_t ctx;
203
204 if (copy_from_user(&ctx, (drm_ctx_t *)arg, sizeof(ctx)))
205 return -EFAULT;
206 DRM_DEBUG("%d\n", ctx.handle);
207 if(ctx.handle != DRM_KERNEL_CONTEXT) {
208 drm_ctxbitmap_free(dev, ctx.handle);
209 }
210
211 return 0;
212 }
213