Merged mga branch with trunk
[platform/upstream/libdrm.git] / linux / i810_context.c
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  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation
10  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11  * and/or sell copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following conditions:
13  * 
14  * The above copyright notice and this permission notice (including the next
15  * paragraph) shall be included in all copies or substantial portions of the
16  * Software.
17  * 
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
21  * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  * 
26  * Author: Rickard E. (Rik) Faith <faith@precisioninsight.com>
27  *
28  * $XFree86$
29  *
30  */
31
32 #include <linux/sched.h>
33
34 #define __NO_VERSION__
35 #include "drmP.h"
36 #include "i810_drv.h"
37
38 static int i810_alloc_queue(drm_device_t *dev)
39 {
40         int temp = drm_ctxbitmap_next(dev);
41         DRM_DEBUG("i810_alloc_queue: %d\n", temp);
42         return temp;
43 }
44
45 int i810_context_switch(drm_device_t *dev, int old, int new)
46 {
47         char        buf[64];
48
49         atomic_inc(&dev->total_ctx);
50
51         if (test_and_set_bit(0, &dev->context_flag)) {
52                 DRM_ERROR("Reentering -- FIXME\n");
53                 return -EBUSY;
54         }
55
56 #if DRM_DMA_HISTOGRAM
57         dev->ctx_start = get_cycles();
58 #endif
59         
60         DRM_DEBUG("Context switch from %d to %d\n", old, new);
61
62         if (new == dev->last_context) {
63                 clear_bit(0, &dev->context_flag);
64                 return 0;
65         }
66         
67         if (drm_flags & DRM_FLAG_NOCTX) {
68                 i810_context_switch_complete(dev, new);
69         } else {
70                 sprintf(buf, "C %d %d\n", old, new);
71                 drm_write_string(dev, buf);
72         }
73         
74         return 0;
75 }
76
77 int i810_context_switch_complete(drm_device_t *dev, int new)
78 {
79         dev->last_context = new;  /* PRE/POST: This is the _only_ writer. */
80         dev->last_switch  = jiffies;
81         
82         if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock)) {
83                 DRM_ERROR("Lock isn't held after context switch\n");
84         }
85
86                                 /* If a context switch is ever initiated
87                                    when the kernel holds the lock, release
88                                    that lock here. */
89 #if DRM_DMA_HISTOGRAM
90         atomic_inc(&dev->histo.ctx[drm_histogram_slot(get_cycles()
91                                                       - dev->ctx_start)]);
92                    
93 #endif
94         clear_bit(0, &dev->context_flag);
95         wake_up(&dev->context_wait);
96         
97         return 0;
98 }
99
100 int i810_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         copy_from_user_ret(&res, (drm_ctx_res_t *)arg, sizeof(res), -EFAULT);
109         if (res.count >= DRM_RESERVED_CONTEXTS) {
110                 memset(&ctx, 0, sizeof(ctx));
111                 for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
112                         ctx.handle = i;
113                         copy_to_user_ret(&res.contexts[i],
114                                          &i,
115                                          sizeof(i),
116                                          -EFAULT);
117                 }
118         }
119         res.count = DRM_RESERVED_CONTEXTS;
120         copy_to_user_ret((drm_ctx_res_t *)arg, &res, sizeof(res), -EFAULT);
121         return 0;
122 }
123
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         copy_from_user_ret(&ctx, (drm_ctx_t *)arg, sizeof(ctx), -EFAULT);
132         if ((ctx.handle = i810_alloc_queue(dev)) == DRM_KERNEL_CONTEXT) {
133                                 /* Skip kernel's context and get a new one. */
134                 ctx.handle = i810_alloc_queue(dev);
135         }
136         if (ctx.handle == -1) {
137                 DRM_DEBUG("Not enough free contexts.\n");
138                                 /* Should this return -EBUSY instead? */
139                 return -ENOMEM;
140         }
141         DRM_DEBUG("%d\n", ctx.handle);
142         copy_to_user_ret((drm_ctx_t *)arg, &ctx, sizeof(ctx), -EFAULT);
143         return 0;
144 }
145
146 int i810_modctx(struct inode *inode, struct file *filp, unsigned int cmd,
147         unsigned long arg)
148 {
149         /* This does nothing for the i810 */
150         return 0;
151 }
152
153 int i810_getctx(struct inode *inode, struct file *filp, unsigned int cmd,
154         unsigned long arg)
155 {
156         drm_ctx_t ctx;
157
158         copy_from_user_ret(&ctx, (drm_ctx_t*)arg, sizeof(ctx), -EFAULT);
159         /* This is 0, because we don't hanlde any context flags */
160         ctx.flags = 0;
161         copy_to_user_ret((drm_ctx_t*)arg, &ctx, sizeof(ctx), -EFAULT);
162         return 0;
163 }
164
165 int i810_switchctx(struct inode *inode, struct file *filp, unsigned int cmd,
166                    unsigned long arg)
167 {
168         drm_file_t      *priv   = filp->private_data;
169         drm_device_t    *dev    = priv->dev;
170         drm_ctx_t       ctx;
171
172         copy_from_user_ret(&ctx, (drm_ctx_t *)arg, sizeof(ctx), -EFAULT);
173         DRM_DEBUG("%d\n", ctx.handle);
174         return i810_context_switch(dev, dev->last_context, ctx.handle);
175 }
176
177 int i810_newctx(struct inode *inode, struct file *filp, unsigned int cmd,
178                 unsigned long arg)
179 {
180         drm_file_t      *priv   = filp->private_data;
181         drm_device_t    *dev    = priv->dev;
182         drm_ctx_t       ctx;
183
184         copy_from_user_ret(&ctx, (drm_ctx_t *)arg, sizeof(ctx), -EFAULT);
185         DRM_DEBUG("%d\n", ctx.handle);
186         i810_context_switch_complete(dev, ctx.handle);
187
188         return 0;
189 }
190
191 int i810_rmctx(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         copy_from_user_ret(&ctx, (drm_ctx_t *)arg, sizeof(ctx), -EFAULT);
199         DRM_DEBUG("%d\n", ctx.handle);
200         if(ctx.handle != DRM_KERNEL_CONTEXT) {
201                 drm_ctxbitmap_free(dev, ctx.handle);
202         }
203         
204         return 0;
205 }