1 /* mga_context.c -- IOCTLs for mga 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  * Author: 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 "mga_drv.h"
35 
mga_alloc_queue(drm_device_t * dev)36 static int mga_alloc_queue(drm_device_t *dev)
37 {
38    	return drm_ctxbitmap_next(dev);
39 }
40 
mga_context_switch(drm_device_t * dev,int old,int new)41 int mga_context_switch(drm_device_t *dev, int old, int new)
42 {
43         char        buf[64];
44 
45         atomic_inc(&dev->total_ctx);
46 
47         if (test_and_set_bit(0, &dev->context_flag)) {
48                 DRM_ERROR("Reentering -- FIXME\n");
49                 return -EBUSY;
50         }
51 
52 #if DRM_DMA_HISTOGRAM
53         dev->ctx_start = get_cycles();
54 #endif
55 
56         DRM_DEBUG("Context switch from %d to %d\n", old, new);
57 
58         if (new == dev->last_context) {
59                 clear_bit(0, &dev->context_flag);
60                 return 0;
61         }
62 
63         if (drm_flags & DRM_FLAG_NOCTX) {
64                 mga_context_switch_complete(dev, new);
65         } else {
66                 sprintf(buf, "C %d %d\n", old, new);
67                 drm_write_string(dev, buf);
68         }
69 
70         return 0;
71 }
72 
mga_context_switch_complete(drm_device_t * dev,int new)73 int mga_context_switch_complete(drm_device_t *dev, int new)
74 {
75         dev->last_context = new;  /* PRE/POST: This is the _only_ writer. */
76         dev->last_switch  = jiffies;
77 
78         if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock)) {
79                 DRM_ERROR("Lock isn't held after context switch\n");
80         }
81 
82 				/* If a context switch is ever initiated
83                                    when the kernel holds the lock, release
84                                    that lock here. */
85 #if DRM_DMA_HISTOGRAM
86         atomic_inc(&dev->histo.ctx[drm_histogram_slot(get_cycles()
87                                                       - dev->ctx_start)]);
88 
89 #endif
90         clear_bit(0, &dev->context_flag);
91         wake_up(&dev->context_wait);
92 
93         return 0;
94 }
95 
mga_resctx(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)96 int mga_resctx(struct inode *inode, struct file *filp, unsigned int cmd,
97 	       unsigned long arg)
98 {
99 	drm_ctx_res_t	res;
100 	drm_ctx_t	ctx;
101 	int		i;
102 
103 	if (copy_from_user(&res, (drm_ctx_res_t *)arg, sizeof(res)))
104 		return -EFAULT;
105 	if (res.count >= DRM_RESERVED_CONTEXTS) {
106 		memset(&ctx, 0, sizeof(ctx));
107 		for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
108 			ctx.handle = i;
109 			if (copy_to_user(&res.contexts[i],
110 					 &i,
111 					 sizeof(i)))
112 				return -EFAULT;
113 		}
114 	}
115 	res.count = DRM_RESERVED_CONTEXTS;
116 	if (copy_to_user((drm_ctx_res_t *)arg, &res, sizeof(res)))
117 		return -EFAULT;
118 	return 0;
119 }
120 
mga_addctx(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)121 int mga_addctx(struct inode *inode, struct file *filp, unsigned int cmd,
122 	       unsigned long arg)
123 {
124 	drm_file_t	*priv	= filp->private_data;
125 	drm_device_t	*dev	= priv->dev;
126 	drm_ctx_t	ctx;
127 
128 	if (copy_from_user(&ctx, (drm_ctx_t *)arg, sizeof(ctx)))
129 		return -EFAULT;
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);
133 	}
134         if (ctx.handle == -1) {
135 		return -ENOMEM;
136 	}
137 	DRM_DEBUG("%d\n", ctx.handle);
138 	if (copy_to_user((drm_ctx_t *)arg, &ctx, sizeof(ctx)))
139 		return -EFAULT;
140 	return 0;
141 }
142 
mga_modctx(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)143 int mga_modctx(struct inode *inode, struct file *filp, unsigned int cmd,
144 	unsigned long arg)
145 {
146    	/* This does nothing for the mga */
147 	return 0;
148 }
149 
mga_getctx(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)150 int mga_getctx(struct inode *inode, struct file *filp, unsigned int cmd,
151 	unsigned long arg)
152 {
153 	drm_ctx_t ctx;
154 
155 	if (copy_from_user(&ctx, (drm_ctx_t*)arg, sizeof(ctx)))
156 		return -EFAULT;
157 	/* This is 0, because we don't hanlde any context flags */
158 	ctx.flags = 0;
159 	if (copy_to_user((drm_ctx_t*)arg, &ctx, sizeof(ctx)))
160 		return -EFAULT;
161 	return 0;
162 }
163 
mga_switchctx(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)164 int mga_switchctx(struct inode *inode, struct file *filp, unsigned int cmd,
165 		   unsigned long arg)
166 {
167 	drm_file_t	*priv	= filp->private_data;
168 	drm_device_t	*dev	= priv->dev;
169 	drm_ctx_t	ctx;
170 
171 	if (copy_from_user(&ctx, (drm_ctx_t *)arg, sizeof(ctx)))
172 		return -EFAULT;
173 	DRM_DEBUG("%d\n", ctx.handle);
174 	return mga_context_switch(dev, dev->last_context, ctx.handle);
175 }
176 
mga_newctx(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)177 int mga_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 	if (copy_from_user(&ctx, (drm_ctx_t *)arg, sizeof(ctx)))
185 		return -EFAULT;
186 	DRM_DEBUG("%d\n", ctx.handle);
187 	mga_context_switch_complete(dev, ctx.handle);
188 
189 	return 0;
190 }
191 
mga_rmctx(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)192 int mga_rmctx(struct inode *inode, struct file *filp, unsigned int cmd,
193 	      unsigned long arg)
194 {
195 	drm_file_t	*priv	= filp->private_data;
196 	drm_device_t	*dev	= priv->dev;
197 	drm_ctx_t	ctx;
198 
199 	if (copy_from_user(&ctx, (drm_ctx_t *)arg, sizeof(ctx)))
200 		return -EFAULT;
201 	DRM_DEBUG("%d\n", ctx.handle);
202 	if(ctx.handle == DRM_KERNEL_CONTEXT+1) priv->remove_auth_on_close = 1;
203 
204       	if(ctx.handle != DRM_KERNEL_CONTEXT) {
205 	   	drm_ctxbitmap_free(dev, ctx.handle);
206 	}
207 
208 	return 0;
209 }
210