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