* Add this nice filesystem testing tool that I've recently
[dragonfly.git] / sys / dev / drm / drm_context.h
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  * Copyright 1999, 2000 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  * VA LINUX SYSTEMS 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
25  * OTHER DEALINGS IN THE SOFTWARE.
26  *
27  * Authors:
28  *    Rickard E. (Rik) Faith <faith@valinux.com>
29  *    Gareth Hughes <gareth@valinux.com>
30  * $FreeBSD: src/sys/dev/drm/drm_context.h,v 1.4.2.1 2003/04/26 07:05:28 anholt Exp $
31  * $DragonFly: src/sys/dev/drm/Attic/drm_context.h,v 1.2 2003/06/17 04:28:24 dillon Exp $
32  */
33
34 #include "dev/drm/drmP.h"
35
36 #if !__HAVE_CTX_BITMAP
37 #error "__HAVE_CTX_BITMAP must be defined"
38 #endif
39
40 /* ================================================================
41  * Context bitmap support
42  */
43
44 void DRM(ctxbitmap_free)( drm_device_t *dev, int ctx_handle )
45 {
46         if ( ctx_handle < 0 ) goto failed;
47         if ( !dev->ctx_bitmap ) goto failed;
48
49         if ( ctx_handle < DRM_MAX_CTXBITMAP ) {
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 failed:
57         DRM_ERROR( "Attempt to free invalid context handle: %d\n",
58                    ctx_handle );
59         return;
60 }
61
62 int DRM(ctxbitmap_next)( drm_device_t *dev )
63 {
64         int bit;
65
66         if(!dev->ctx_bitmap) return -1;
67
68         DRM_LOCK;
69         bit = find_first_zero_bit( dev->ctx_bitmap, DRM_MAX_CTXBITMAP );
70         if ( bit < DRM_MAX_CTXBITMAP ) {
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) {
76                                 drm_local_map_t **ctx_sareas;
77
78                                 ctx_sareas = DRM(realloc)(dev->context_sareas,
79                                                 (dev->max_context - 1) * 
80                                                 sizeof(*dev->context_sareas),
81                                                 dev->max_context * 
82                                                 sizeof(*dev->context_sareas),
83                                                 DRM_MEM_MAPS);
84                                 if(!ctx_sareas) {
85                                         clear_bit(bit, dev->ctx_bitmap);
86                                         DRM_UNLOCK;
87                                         return -1;
88                                 }
89                                 dev->context_sareas = ctx_sareas;
90                                 dev->context_sareas[bit] = NULL;
91                         } else {
92                                 /* max_context == 1 at this point */
93                                 dev->context_sareas = DRM(alloc)(
94                                                 dev->max_context * 
95                                                 sizeof(*dev->context_sareas),
96                                                 DRM_MEM_MAPS);
97                                 if(!dev->context_sareas) {
98                                         clear_bit(bit, dev->ctx_bitmap);
99                                         DRM_UNLOCK;
100                                         return -1;
101                                 }
102                                 dev->context_sareas[bit] = NULL;
103                         }
104                 }
105                 DRM_UNLOCK;
106                 return bit;
107         }
108         DRM_UNLOCK;
109         return -1;
110 }
111
112 int DRM(ctxbitmap_init)( drm_device_t *dev )
113 {
114         int i;
115         int temp;
116
117         DRM_LOCK;
118         dev->ctx_bitmap = (atomic_t *) DRM(alloc)( PAGE_SIZE,
119                                                         DRM_MEM_CTXBITMAP );
120         if ( dev->ctx_bitmap == NULL ) {
121                 DRM_UNLOCK;
122                 return DRM_ERR(ENOMEM);
123         }
124         memset( (void *)dev->ctx_bitmap, 0, PAGE_SIZE );
125         dev->context_sareas = NULL;
126         dev->max_context = -1;
127         DRM_UNLOCK;
128
129         for ( i = 0 ; i < DRM_RESERVED_CONTEXTS ; i++ ) {
130                 temp = DRM(ctxbitmap_next)( dev );
131                 DRM_DEBUG( "drm_ctxbitmap_init : %d\n", temp );
132         }
133
134         return 0;
135 }
136
137 void DRM(ctxbitmap_cleanup)( drm_device_t *dev )
138 {
139         DRM_LOCK;
140         if( dev->context_sareas ) DRM(free)( dev->context_sareas,
141                                              sizeof(*dev->context_sareas) * 
142                                              dev->max_context,
143                                              DRM_MEM_MAPS );
144         DRM(free)( (void *)dev->ctx_bitmap, PAGE_SIZE, DRM_MEM_CTXBITMAP );
145         DRM_UNLOCK;
146 }
147
148 /* ================================================================
149  * Per Context SAREA Support
150  */
151
152 int DRM(getsareactx)( DRM_IOCTL_ARGS )
153 {
154         DRM_DEVICE;
155         drm_ctx_priv_map_t request;
156         drm_local_map_t *map;
157
158         DRM_COPY_FROM_USER_IOCTL( request, (drm_ctx_priv_map_t *)data, 
159                            sizeof(request) );
160
161         DRM_LOCK;
162         if (dev->max_context < 0 || request.ctx_id >= (unsigned) dev->max_context) {
163                 DRM_UNLOCK;
164                 return DRM_ERR(EINVAL);
165         }
166
167         map = dev->context_sareas[request.ctx_id];
168         DRM_UNLOCK;
169
170         request.handle = map->handle;
171
172         DRM_COPY_TO_USER_IOCTL( (drm_ctx_priv_map_t *)data, request, sizeof(request) );
173
174         return 0;
175 }
176
177 int DRM(setsareactx)( DRM_IOCTL_ARGS )
178 {
179         DRM_DEVICE;
180         drm_ctx_priv_map_t request;
181         drm_local_map_t *map = NULL;
182         drm_map_list_entry_t *list;
183
184         DRM_COPY_FROM_USER_IOCTL( request, (drm_ctx_priv_map_t *)data,
185                            sizeof(request) );
186
187         DRM_LOCK;
188         TAILQ_FOREACH(list, dev->maplist, link) {
189                 map=list->map;
190                 if(map->handle == request.handle) {
191                         if (dev->max_context < 0)
192                                 goto bad;
193                         if (request.ctx_id >= (unsigned) dev->max_context)
194                                 goto bad;
195                         dev->context_sareas[request.ctx_id] = map;
196                         DRM_UNLOCK;
197                         return 0;
198                 }
199         }
200
201 bad:
202         DRM_UNLOCK;
203         return DRM_ERR(EINVAL);
204 }
205
206 /* ================================================================
207  * The actual DRM context handling routines
208  */
209
210 int DRM(context_switch)( drm_device_t *dev, int old, int new )
211 {
212         if ( test_and_set_bit( 0, &dev->context_flag ) ) {
213                 DRM_ERROR( "Reentering -- FIXME\n" );
214                 return DRM_ERR(EBUSY);
215         }
216
217         DRM_DEBUG( "Context switch from %d to %d\n", old, new );
218
219         if ( new == dev->last_context ) {
220                 clear_bit( 0, &dev->context_flag );
221                 return 0;
222         }
223
224         return 0;
225 }
226
227 int DRM(context_switch_complete)( drm_device_t *dev, int new )
228 {
229         dev->last_context = new;  /* PRE/POST: This is the _only_ writer. */
230
231         if ( !_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) ) {
232                 DRM_ERROR( "Lock isn't held after context switch\n" );
233         }
234
235                                 /* If a context switch is ever initiated
236                                    when the kernel holds the lock, release
237                                    that lock here. */
238         clear_bit( 0, &dev->context_flag );
239
240         return 0;
241 }
242
243 int DRM(resctx)( DRM_IOCTL_ARGS )
244 {
245         drm_ctx_res_t res;
246         drm_ctx_t ctx;
247         int i;
248
249         DRM_COPY_FROM_USER_IOCTL( res, (drm_ctx_res_t *)data, sizeof(res) );
250
251         if ( res.count >= DRM_RESERVED_CONTEXTS ) {
252                 memset( &ctx, 0, sizeof(ctx) );
253                 for ( i = 0 ; i < DRM_RESERVED_CONTEXTS ; i++ ) {
254                         ctx.handle = i;
255                         if ( DRM_COPY_TO_USER( &res.contexts[i],
256                                            &i, sizeof(i) ) )
257                                 return DRM_ERR(EFAULT);
258                 }
259         }
260         res.count = DRM_RESERVED_CONTEXTS;
261
262         DRM_COPY_TO_USER_IOCTL( (drm_ctx_res_t *)data, res, sizeof(res) );
263
264         return 0;
265 }
266
267 int DRM(addctx)( DRM_IOCTL_ARGS )
268 {
269         DRM_DEVICE;
270         drm_ctx_t ctx;
271
272         DRM_COPY_FROM_USER_IOCTL( ctx, (drm_ctx_t *)data, sizeof(ctx) );
273
274         ctx.handle = DRM(ctxbitmap_next)( dev );
275         if ( ctx.handle == DRM_KERNEL_CONTEXT ) {
276                                 /* Skip kernel's context and get a new one. */
277                 ctx.handle = DRM(ctxbitmap_next)( dev );
278         }
279         DRM_DEBUG( "%d\n", ctx.handle );
280         if ( ctx.handle == -1 ) {
281                 DRM_DEBUG( "Not enough free contexts.\n" );
282                                 /* Should this return -EBUSY instead? */
283                 return DRM_ERR(ENOMEM);
284         }
285
286         DRM_COPY_TO_USER_IOCTL( (drm_ctx_t *)data, ctx, sizeof(ctx) );
287
288         return 0;
289 }
290
291 int DRM(modctx)( DRM_IOCTL_ARGS )
292 {
293         /* This does nothing */
294         return 0;
295 }
296
297 int DRM(getctx)( DRM_IOCTL_ARGS )
298 {
299         drm_ctx_t ctx;
300
301         DRM_COPY_FROM_USER_IOCTL( ctx, (drm_ctx_t *)data, sizeof(ctx) );
302
303         /* This is 0, because we don't handle any context flags */
304         ctx.flags = 0;
305
306         DRM_COPY_TO_USER_IOCTL( (drm_ctx_t *)data, ctx, sizeof(ctx) );
307
308         return 0;
309 }
310
311 int DRM(switchctx)( 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         return DRM(context_switch)( dev, dev->last_context, ctx.handle );
320 }
321
322 int DRM(newctx)( DRM_IOCTL_ARGS )
323 {
324         DRM_DEVICE;
325         drm_ctx_t ctx;
326
327         DRM_COPY_FROM_USER_IOCTL( ctx, (drm_ctx_t *)data, sizeof(ctx) );
328
329         DRM_DEBUG( "%d\n", ctx.handle );
330         DRM(context_switch_complete)( dev, ctx.handle );
331
332         return 0;
333 }
334
335 int DRM(rmctx)( DRM_IOCTL_ARGS )
336 {
337         DRM_DEVICE;
338         drm_ctx_t ctx;
339
340         DRM_COPY_FROM_USER_IOCTL( ctx, (drm_ctx_t *)data, sizeof(ctx) );
341
342         DRM_DEBUG( "%d\n", ctx.handle );
343         if ( ctx.handle != DRM_KERNEL_CONTEXT ) {
344                 DRM(ctxbitmap_free)( dev, ctx.handle );
345         }
346
347         return 0;
348 }