OSDN Git Service

[FreeBSD] Add drm_drawable_free_all()
[android-x86/external-libdrm.git] / bsd-core / drm_context.c
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  */
30
31 /** @file drm_context.c
32  * Implementation of the context management ioctls.
33  */
34
35 #include "drmP.h"
36
37 /* ================================================================
38  * Context bitmap support
39  */
40
41 void drm_ctxbitmap_free(struct drm_device *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(struct drm_device *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(struct drm_device *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 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(struct drm_device *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(struct drm_device *dev, void *data,
141                     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(struct drm_device *dev, void *data,
162                     struct drm_file *file_priv)
163 {
164         drm_ctx_priv_map_t *request = data;
165         drm_local_map_t *map = NULL;
166
167         DRM_LOCK();
168         TAILQ_FOREACH(map, &dev->maplist, link) {
169                 if (map->handle == request->handle) {
170                         if (dev->max_context < 0)
171                                 goto bad;
172                         if (request->ctx_id >= (unsigned) dev->max_context)
173                                 goto bad;
174                         dev->context_sareas[request->ctx_id] = map;
175                         DRM_UNLOCK();
176                         return 0;
177                 }
178         }
179
180 bad:
181         DRM_UNLOCK();
182         return EINVAL;
183 }
184
185 /* ================================================================
186  * The actual DRM context handling routines
187  */
188
189 int drm_context_switch(struct drm_device *dev, int old, int new)
190 {
191         if ( test_and_set_bit( 0, &dev->context_flag ) ) {
192                 DRM_ERROR( "Reentering -- FIXME\n" );
193                 return EBUSY;
194         }
195
196         DRM_DEBUG( "Context switch from %d to %d\n", old, new );
197
198         if ( new == dev->last_context ) {
199                 clear_bit( 0, &dev->context_flag );
200                 return 0;
201         }
202
203         return 0;
204 }
205
206 int drm_context_switch_complete(struct drm_device *dev, int new)
207 {
208         dev->last_context = new;  /* PRE/POST: This is the _only_ writer. */
209
210         if ( !_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) ) {
211                 DRM_ERROR( "Lock isn't held after context switch\n" );
212         }
213
214                                 /* If a context switch is ever initiated
215                                    when the kernel holds the lock, release
216                                    that lock here. */
217         clear_bit( 0, &dev->context_flag );
218
219         return 0;
220 }
221
222 int drm_resctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
223 {
224         drm_ctx_res_t *res = data;
225         drm_ctx_t ctx;
226         int i;
227
228         if ( res->count >= DRM_RESERVED_CONTEXTS ) {
229                 bzero(&ctx, sizeof(ctx));
230                 for ( i = 0 ; i < DRM_RESERVED_CONTEXTS ; i++ ) {
231                         ctx.handle = i;
232                         if ( DRM_COPY_TO_USER( &res->contexts[i],
233                                            &ctx, sizeof(ctx) ) )
234                                 return EFAULT;
235                 }
236         }
237         res->count = DRM_RESERVED_CONTEXTS;
238
239         return 0;
240 }
241
242 int drm_addctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
243 {
244         drm_ctx_t *ctx = data;
245
246         ctx->handle = drm_ctxbitmap_next(dev);
247         if ( ctx->handle == DRM_KERNEL_CONTEXT ) {
248                                 /* Skip kernel's context and get a new one. */
249                 ctx->handle = drm_ctxbitmap_next(dev);
250         }
251         DRM_DEBUG( "%d\n", ctx->handle );
252         if ( ctx->handle == -1 ) {
253                 DRM_DEBUG( "Not enough free contexts.\n" );
254                                 /* Should this return -EBUSY instead? */
255                 return ENOMEM;
256         }
257
258         if (dev->driver.context_ctor && ctx->handle != DRM_KERNEL_CONTEXT) {
259                 DRM_LOCK();
260                 dev->driver.context_ctor(dev, ctx->handle);
261                 DRM_UNLOCK();
262         }
263
264         return 0;
265 }
266
267 int drm_modctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
268 {
269         /* This does nothing */
270         return 0;
271 }
272
273 int drm_getctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
274 {
275         drm_ctx_t *ctx = data;
276
277         /* This is 0, because we don't handle any context flags */
278         ctx->flags = 0;
279
280         return 0;
281 }
282
283 int drm_switchctx(struct drm_device *dev, void *data,
284                   struct drm_file *file_priv)
285 {
286         drm_ctx_t *ctx = data;
287
288         DRM_DEBUG( "%d\n", ctx->handle );
289         return drm_context_switch(dev, dev->last_context, ctx->handle);
290 }
291
292 int drm_newctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
293 {
294         drm_ctx_t *ctx = data;
295
296         DRM_DEBUG( "%d\n", ctx->handle );
297         drm_context_switch_complete(dev, ctx->handle);
298
299         return 0;
300 }
301
302 int drm_rmctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
303 {
304         drm_ctx_t *ctx = data;
305
306         DRM_DEBUG( "%d\n", ctx->handle );
307         if ( ctx->handle != DRM_KERNEL_CONTEXT ) {
308                 if (dev->driver.context_dtor) {
309                         DRM_LOCK();
310                         dev->driver.context_dtor(dev, ctx->handle);
311                         DRM_UNLOCK();
312                 }
313
314                 drm_ctxbitmap_free(dev, ctx->handle);
315         }
316
317         return 0;
318 }