xref: /dragonfly/sys/dev/drm/drm_context.c (revision 62f7f702)
1 /*-
2  * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
3  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23  * OTHER DEALINGS IN THE SOFTWARE.
24  *
25  * Authors:
26  *    Rickard E. (Rik) Faith <faith@valinux.com>
27  *    Gareth Hughes <gareth@valinux.com>
28  *
29  * $DragonFly: src/sys/dev/drm/drm_context.c,v 1.1 2008/04/05 18:12:29 hasso Exp $
30  */
31 
32 /** @file drm_context.c
33  * Implementation of the context management ioctls.
34  */
35 
36 #include "drmP.h"
37 
38 /* ================================================================
39  * Context bitmap support
40  */
41 
42 void drm_ctxbitmap_free(drm_device_t *dev, int ctx_handle)
43 {
44 	if (ctx_handle < 0 || ctx_handle >= DRM_MAX_CTXBITMAP ||
45 	    dev->ctx_bitmap == NULL) {
46 		DRM_ERROR("Attempt to free invalid context handle: %d\n",
47 		   ctx_handle);
48 		return;
49 	}
50 
51 	DRM_LOCK();
52 	clear_bit(ctx_handle, dev->ctx_bitmap);
53 	dev->context_sareas[ctx_handle] = NULL;
54 	DRM_UNLOCK();
55 	return;
56 }
57 
58 int drm_ctxbitmap_next(drm_device_t *dev)
59 {
60 	int bit;
61 
62 	if (dev->ctx_bitmap == NULL)
63 		return -1;
64 
65 	DRM_LOCK();
66 	bit = find_first_zero_bit( dev->ctx_bitmap, DRM_MAX_CTXBITMAP );
67 	if (bit >= DRM_MAX_CTXBITMAP) {
68 		DRM_UNLOCK();
69 		return -1;
70 	}
71 
72 	set_bit(bit, dev->ctx_bitmap);
73 	DRM_DEBUG("drm_ctxbitmap_next bit : %d\n", bit);
74 	if ((bit+1) > dev->max_context) {
75 		dev->max_context = (bit+1);
76 		if (dev->context_sareas != NULL) {
77 			drm_local_map_t **ctx_sareas;
78 
79 			ctx_sareas = realloc(dev->context_sareas,
80 			    dev->max_context * sizeof(*dev->context_sareas),
81 			    M_DRM, M_NOWAIT);
82 			if (ctx_sareas == NULL) {
83 				clear_bit(bit, dev->ctx_bitmap);
84 				DRM_UNLOCK();
85 				return -1;
86 			}
87 			dev->context_sareas = ctx_sareas;
88 			dev->context_sareas[bit] = NULL;
89 		} else {
90 			/* max_context == 1 at this point */
91 			dev->context_sareas = malloc(dev->max_context *
92 			    sizeof(*dev->context_sareas), M_DRM, M_NOWAIT);
93 			if (dev->context_sareas == NULL) {
94 				clear_bit(bit, dev->ctx_bitmap);
95 				DRM_UNLOCK();
96 				return -1;
97 			}
98 			dev->context_sareas[bit] = NULL;
99 		}
100 	}
101 	DRM_UNLOCK();
102 	return bit;
103 }
104 
105 int drm_ctxbitmap_init(drm_device_t *dev)
106 {
107 	int i;
108    	int temp;
109 
110 	DRM_LOCK();
111 	dev->ctx_bitmap = malloc(PAGE_SIZE, M_DRM, M_NOWAIT | M_ZERO);
112 	if ( dev->ctx_bitmap == NULL ) {
113 		DRM_UNLOCK();
114 		return ENOMEM;
115 	}
116 	dev->context_sareas = NULL;
117 	dev->max_context = -1;
118 	DRM_UNLOCK();
119 
120 	for ( i = 0 ; i < DRM_RESERVED_CONTEXTS ; i++ ) {
121 		temp = drm_ctxbitmap_next(dev);
122 	   	DRM_DEBUG( "drm_ctxbitmap_init : %d\n", temp );
123 	}
124 
125 	return 0;
126 }
127 
128 void drm_ctxbitmap_cleanup(drm_device_t *dev)
129 {
130 	DRM_LOCK();
131 	if (dev->context_sareas != NULL)
132 		free(dev->context_sareas, M_DRM);
133 	free(dev->ctx_bitmap, M_DRM);
134 	DRM_UNLOCK();
135 }
136 
137 /* ================================================================
138  * Per Context SAREA Support
139  */
140 
141 int drm_getsareactx( drm_device_t *dev, void *data, struct drm_file *file_priv )
142 {
143 	drm_ctx_priv_map_t *request = data;
144 	drm_local_map_t *map;
145 
146 	DRM_LOCK();
147 	if (dev->max_context < 0 ||
148 	    request->ctx_id >= (unsigned) dev->max_context) {
149 		DRM_UNLOCK();
150 		return EINVAL;
151 	}
152 
153 	map = dev->context_sareas[request->ctx_id];
154 	DRM_UNLOCK();
155 
156 	request->handle = map->handle;
157 
158 	return 0;
159 }
160 
161 int drm_setsareactx(drm_device_t *dev, void *data, struct drm_file *file_priv)
162 {
163 	drm_ctx_priv_map_t *request = data;
164 	drm_local_map_t *map = NULL;
165 
166 	DRM_LOCK();
167 	TAILQ_FOREACH(map, &dev->maplist, link) {
168 		if (map->handle == request->handle) {
169 			if (dev->max_context < 0)
170 				goto bad;
171 			if (request->ctx_id >= (unsigned) dev->max_context)
172 				goto bad;
173 			dev->context_sareas[request->ctx_id] = map;
174 			DRM_UNLOCK();
175 			return 0;
176 		}
177 	}
178 
179 bad:
180 	DRM_UNLOCK();
181 	return EINVAL;
182 }
183 
184 /* ================================================================
185  * The actual DRM context handling routines
186  */
187 
188 int drm_context_switch(drm_device_t *dev, int old, int new)
189 {
190         if ( test_and_set_bit( 0, &dev->context_flag ) ) {
191                 DRM_ERROR( "Reentering -- FIXME\n" );
192                 return EBUSY;
193         }
194 
195         DRM_DEBUG( "Context switch from %d to %d\n", old, new );
196 
197         if ( new == dev->last_context ) {
198                 clear_bit( 0, &dev->context_flag );
199                 return 0;
200         }
201 
202         return 0;
203 }
204 
205 int drm_context_switch_complete(drm_device_t *dev, int new)
206 {
207         dev->last_context = new;  /* PRE/POST: This is the _only_ writer. */
208 
209         if ( !_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) ) {
210                 DRM_ERROR( "Lock isn't held after context switch\n" );
211         }
212 
213 				/* If a context switch is ever initiated
214                                    when the kernel holds the lock, release
215                                    that lock here. */
216         clear_bit( 0, &dev->context_flag );
217 
218         return 0;
219 }
220 
221 int drm_resctx(drm_device_t *dev, void *data, struct drm_file *file_priv)
222 {
223 	drm_ctx_res_t *res = data;
224 	drm_ctx_t ctx;
225 	int i;
226 
227 	if ( res->count >= DRM_RESERVED_CONTEXTS ) {
228 		bzero(&ctx, sizeof(ctx));
229 		for ( i = 0 ; i < DRM_RESERVED_CONTEXTS ; i++ ) {
230 			ctx.handle = i;
231 			if ( DRM_COPY_TO_USER( &res->contexts[i],
232 					   &ctx, sizeof(ctx) ) )
233 				return EFAULT;
234 		}
235 	}
236 	res->count = DRM_RESERVED_CONTEXTS;
237 
238 	return 0;
239 }
240 
241 int drm_addctx(drm_device_t *dev, void *data, struct drm_file *file_priv)
242 {
243 	drm_ctx_t *ctx = data;
244 
245 	ctx->handle = drm_ctxbitmap_next(dev);
246 	if ( ctx->handle == DRM_KERNEL_CONTEXT ) {
247 				/* Skip kernel's context and get a new one. */
248 		ctx->handle = drm_ctxbitmap_next(dev);
249 	}
250 	DRM_DEBUG( "%d\n", ctx->handle );
251 	if ( ctx->handle == -1 ) {
252 		DRM_DEBUG( "Not enough free contexts.\n" );
253 				/* Should this return -EBUSY instead? */
254 		return ENOMEM;
255 	}
256 
257 	if (dev->driver.context_ctor && ctx->handle != DRM_KERNEL_CONTEXT) {
258 		DRM_LOCK();
259 		dev->driver.context_ctor(dev, ctx->handle);
260 		DRM_UNLOCK();
261 	}
262 
263 	return 0;
264 }
265 
266 int drm_modctx(drm_device_t *dev, void *data, struct drm_file *file_priv)
267 {
268 	/* This does nothing */
269 	return 0;
270 }
271 
272 int drm_getctx(drm_device_t *dev, void *data, struct drm_file *file_priv)
273 {
274 	drm_ctx_t *ctx = data;
275 
276 	/* This is 0, because we don't handle any context flags */
277 	ctx->flags = 0;
278 
279 	return 0;
280 }
281 
282 int drm_switchctx(drm_device_t *dev, void *data, struct drm_file *file_priv)
283 {
284 	drm_ctx_t *ctx = data;
285 
286 	DRM_DEBUG( "%d\n", ctx->handle );
287 	return drm_context_switch(dev, dev->last_context, ctx->handle);
288 }
289 
290 int drm_newctx(drm_device_t *dev, void *data, struct drm_file *file_priv)
291 {
292 	drm_ctx_t *ctx = data;
293 
294 	DRM_DEBUG( "%d\n", ctx->handle );
295 	drm_context_switch_complete(dev, ctx->handle);
296 
297 	return 0;
298 }
299 
300 int drm_rmctx(drm_device_t *dev, void *data, struct drm_file *file_priv)
301 {
302 	drm_ctx_t *ctx = data;
303 
304 	DRM_DEBUG( "%d\n", ctx->handle );
305 	if ( ctx->handle != DRM_KERNEL_CONTEXT ) {
306 		if (dev->driver.context_dtor) {
307 			DRM_LOCK();
308 			dev->driver.context_dtor(dev, ctx->handle);
309 			DRM_UNLOCK();
310 		}
311 
312 		drm_ctxbitmap_free(dev, ctx->handle);
313 	}
314 
315 	return 0;
316 }
317