1 /* radeon_context.c -- IOCTLs for Radeon contexts -*- linux-c -*-
2  *
3  * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
4  * Copyright 2000 VA Linux Systems, Inc., Fremont, California.
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: Kevin E. Martin <martin@valinux.com>
27  *         Rickard E. (Rik) Faith <faith@valinux.com>
28  *
29  */
30 
31 #define __NO_VERSION__
32 #include "drmP.h"
33 #include "radeon_drv.h"
34 
35 extern drm_ctx_t radeon_res_ctx;
36 
radeon_alloc_queue(drm_device_t * dev)37 static int radeon_alloc_queue(drm_device_t *dev)
38 {
39 	return drm_ctxbitmap_next(dev);
40 }
41 
radeon_context_switch(drm_device_t * dev,int old,int new)42 int radeon_context_switch(drm_device_t *dev, int old, int new)
43 {
44         char        buf[64];
45 
46         atomic_inc(&dev->total_ctx);
47 
48         if (test_and_set_bit(0, &dev->context_flag)) {
49                 DRM_ERROR("Reentering -- FIXME\n");
50                 return -EBUSY;
51         }
52 
53 #if DRM_DMA_HISTOGRAM
54         dev->ctx_start = get_cycles();
55 #endif
56 
57         DRM_DEBUG("Context switch from %d to %d\n", old, new);
58 
59         if (new == dev->last_context) {
60                 clear_bit(0, &dev->context_flag);
61                 return 0;
62         }
63 
64         if (drm_flags & DRM_FLAG_NOCTX) {
65                 radeon_context_switch_complete(dev, new);
66         } else {
67                 sprintf(buf, "C %d %d\n", old, new);
68                 drm_write_string(dev, buf);
69         }
70 
71         return 0;
72 }
73 
radeon_context_switch_complete(drm_device_t * dev,int new)74 int radeon_context_switch_complete(drm_device_t *dev, int new)
75 {
76         dev->last_context = new;  /* PRE/POST: This is the _only_ writer. */
77         dev->last_switch  = jiffies;
78 
79         if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock)) {
80                 DRM_ERROR("Lock isn't held after context switch\n");
81         }
82 
83 				/* If a context switch is ever initiated
84                                    when the kernel holds the lock, release
85                                    that lock here. */
86 #if DRM_DMA_HISTOGRAM
87         atomic_inc(&dev->histo.ctx[drm_histogram_slot(get_cycles()
88                                                       - dev->ctx_start)]);
89 
90 #endif
91         clear_bit(0, &dev->context_flag);
92         wake_up(&dev->context_wait);
93 
94         return 0;
95 }
96 
97 
radeon_resctx(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)98 int radeon_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], &i, sizeof(i)))
113 				return -EFAULT;
114 		}
115 	}
116 	res.count = DRM_RESERVED_CONTEXTS;
117 	if (copy_to_user((drm_ctx_res_t *)arg, &res, sizeof(res)))
118 		return -EFAULT;
119 	return 0;
120 }
121 
122 
radeon_addctx(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)123 int radeon_addctx(struct inode *inode, struct file *filp, unsigned int cmd,
124 		  unsigned long arg)
125 {
126 	drm_file_t	*priv	= filp->private_data;
127 	drm_device_t	*dev	= priv->dev;
128 	drm_ctx_t	ctx;
129 
130 	if (copy_from_user(&ctx, (drm_ctx_t *)arg, sizeof(ctx)))
131 		return -EFAULT;
132 	if ((ctx.handle = radeon_alloc_queue(dev)) == DRM_KERNEL_CONTEXT) {
133 				/* Skip kernel's context and get a new one. */
134 		ctx.handle = radeon_alloc_queue(dev);
135 	}
136 	DRM_DEBUG("%d\n", ctx.handle);
137 	if (ctx.handle == -1) {
138 		DRM_DEBUG("Not enough free contexts.\n");
139 				/* Should this return -EBUSY instead? */
140 		return -ENOMEM;
141 	}
142 
143 	if (copy_to_user((drm_ctx_t *)arg, &ctx, sizeof(ctx)))
144 		return -EFAULT;
145 	return 0;
146 }
147 
radeon_modctx(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)148 int radeon_modctx(struct inode *inode, struct file *filp, unsigned int cmd,
149 		  unsigned long arg)
150 {
151 	drm_ctx_t ctx;
152 
153 	if (copy_from_user(&ctx, (drm_ctx_t*)arg, sizeof(ctx)))
154 		return -EFAULT;
155 	if (ctx.flags==_DRM_CONTEXT_PRESERVED)
156 		radeon_res_ctx.handle=ctx.handle;
157 	return 0;
158 }
159 
radeon_getctx(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)160 int radeon_getctx(struct inode *inode, struct file *filp, unsigned int cmd,
161 		  unsigned long arg)
162 {
163 	drm_ctx_t ctx;
164 
165 	if (copy_from_user(&ctx, (drm_ctx_t*)arg, sizeof(ctx)))
166 		return -EFAULT;
167 	/* This is 0, because we don't hanlde any context flags */
168 	ctx.flags = 0;
169 	if (copy_to_user((drm_ctx_t*)arg, &ctx, sizeof(ctx)))
170 		return -EFAULT;
171 	return 0;
172 }
173 
radeon_switchctx(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)174 int radeon_switchctx(struct inode *inode, struct file *filp, unsigned int cmd,
175 		     unsigned long arg)
176 {
177 	drm_file_t	*priv	= filp->private_data;
178 	drm_device_t	*dev	= priv->dev;
179 	drm_ctx_t	ctx;
180 
181 	if (copy_from_user(&ctx, (drm_ctx_t *)arg, sizeof(ctx)))
182 		return -EFAULT;
183 	DRM_DEBUG("%d\n", ctx.handle);
184 	return radeon_context_switch(dev, dev->last_context, ctx.handle);
185 }
186 
radeon_newctx(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)187 int radeon_newctx(struct inode *inode, struct file *filp, unsigned int cmd,
188 		  unsigned long arg)
189 {
190 	drm_file_t	*priv	= filp->private_data;
191 	drm_device_t	*dev	= priv->dev;
192 	drm_ctx_t	ctx;
193 
194 	if (copy_from_user(&ctx, (drm_ctx_t *)arg, sizeof(ctx)))
195 		return -EFAULT;
196 	DRM_DEBUG("%d\n", ctx.handle);
197 	radeon_context_switch_complete(dev, ctx.handle);
198 
199 	return 0;
200 }
201 
radeon_rmctx(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)202 int radeon_rmctx(struct inode *inode, struct file *filp, unsigned int cmd,
203 		 unsigned long arg)
204 {
205 	drm_file_t	*priv	= filp->private_data;
206 	drm_device_t	*dev	= priv->dev;
207 	drm_ctx_t	ctx;
208 
209 	if (copy_from_user(&ctx, (drm_ctx_t *)arg, sizeof(ctx)))
210 		return -EFAULT;
211 	DRM_DEBUG("%d\n", ctx.handle);
212 	drm_ctxbitmap_free(dev, ctx.handle);
213 
214 	return 0;
215 }
216