1 /* drm_context.h -- IOCTLs for generic contexts -*- linux-c -*-
2 * Created: Fri Nov 24 18:31:37 2000 by gareth@valinux.com
5 * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
6 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
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:
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
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.
29 * Rickard E. (Rik) Faith <faith@valinux.com>
30 * Gareth Hughes <gareth@valinux.com>
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include "dev/drm/drmP.h"
39 /* ================================================================
40 * Context bitmap support
43 void drm_ctxbitmap_free(drm_device_t *dev, int ctx_handle)
45 if (ctx_handle < 0 || ctx_handle >= DRM_MAX_CTXBITMAP ||
46 dev->ctx_bitmap == NULL) {
47 DRM_ERROR("Attempt to free invalid context handle: %d\n",
53 clear_bit(ctx_handle, dev->ctx_bitmap);
54 dev->context_sareas[ctx_handle] = NULL;
59 int drm_ctxbitmap_next(drm_device_t *dev)
63 if (dev->ctx_bitmap == NULL)
67 bit = find_first_zero_bit( dev->ctx_bitmap, DRM_MAX_CTXBITMAP );
68 if (bit >= DRM_MAX_CTXBITMAP) {
73 set_bit(bit, dev->ctx_bitmap);
74 DRM_DEBUG("drm_ctxbitmap_next bit : %d\n", bit);
75 if ((bit+1) > dev->max_context) {
76 dev->max_context = (bit+1);
77 if (dev->context_sareas != NULL) {
78 drm_local_map_t **ctx_sareas;
80 ctx_sareas = realloc(dev->context_sareas,
81 dev->max_context * sizeof(*dev->context_sareas),
83 if (ctx_sareas == NULL) {
84 clear_bit(bit, dev->ctx_bitmap);
88 dev->context_sareas = ctx_sareas;
89 dev->context_sareas[bit] = NULL;
91 /* max_context == 1 at this point */
92 dev->context_sareas = malloc(dev->max_context *
93 sizeof(*dev->context_sareas), M_DRM, M_NOWAIT);
94 if (dev->context_sareas == NULL) {
95 clear_bit(bit, dev->ctx_bitmap);
99 dev->context_sareas[bit] = NULL;
106 int drm_ctxbitmap_init(drm_device_t *dev)
112 dev->ctx_bitmap = malloc(PAGE_SIZE, M_DRM, M_NOWAIT | M_ZERO);
113 if ( dev->ctx_bitmap == NULL ) {
115 return DRM_ERR(ENOMEM);
117 dev->context_sareas = NULL;
118 dev->max_context = -1;
121 for ( i = 0 ; i < DRM_RESERVED_CONTEXTS ; i++ ) {
122 temp = drm_ctxbitmap_next(dev);
123 DRM_DEBUG( "drm_ctxbitmap_init : %d\n", temp );
129 void drm_ctxbitmap_cleanup(drm_device_t *dev)
132 if (dev->context_sareas != NULL)
133 free(dev->context_sareas, M_DRM);
134 free(dev->ctx_bitmap, M_DRM);
138 /* ================================================================
139 * Per Context SAREA Support
142 int drm_getsareactx( DRM_IOCTL_ARGS )
145 drm_ctx_priv_map_t request;
146 drm_local_map_t *map;
148 DRM_COPY_FROM_USER_IOCTL( request, (drm_ctx_priv_map_t *)data,
152 if (dev->max_context < 0 || request.ctx_id >= (unsigned) dev->max_context) {
154 return DRM_ERR(EINVAL);
157 map = dev->context_sareas[request.ctx_id];
160 request.handle = map->handle;
162 DRM_COPY_TO_USER_IOCTL( (drm_ctx_priv_map_t *)data, request, sizeof(request) );
167 int drm_setsareactx( DRM_IOCTL_ARGS )
170 drm_ctx_priv_map_t request;
171 drm_local_map_t *map = NULL;
173 DRM_COPY_FROM_USER_IOCTL( request, (drm_ctx_priv_map_t *)data,
177 TAILQ_FOREACH(map, &dev->maplist, link) {
178 if (map->handle == request.handle) {
179 if (dev->max_context < 0)
181 if (request.ctx_id >= (unsigned) dev->max_context)
183 dev->context_sareas[request.ctx_id] = map;
191 return DRM_ERR(EINVAL);
194 /* ================================================================
195 * The actual DRM context handling routines
198 int drm_context_switch(drm_device_t *dev, int old, int new)
200 if ( test_and_set_bit( 0, &dev->context_flag ) ) {
201 DRM_ERROR( "Reentering -- FIXME\n" );
202 return DRM_ERR(EBUSY);
205 DRM_DEBUG( "Context switch from %d to %d\n", old, new );
207 if ( new == dev->last_context ) {
208 clear_bit( 0, &dev->context_flag );
215 int drm_context_switch_complete(drm_device_t *dev, int new)
217 dev->last_context = new; /* PRE/POST: This is the _only_ writer. */
219 if ( !_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) ) {
220 DRM_ERROR( "Lock isn't held after context switch\n" );
223 /* If a context switch is ever initiated
224 when the kernel holds the lock, release
226 clear_bit( 0, &dev->context_flag );
231 int drm_resctx(DRM_IOCTL_ARGS)
237 DRM_COPY_FROM_USER_IOCTL( res, (drm_ctx_res_t *)data, sizeof(res) );
239 if ( res.count >= DRM_RESERVED_CONTEXTS ) {
240 bzero(&ctx, sizeof(ctx));
241 for ( i = 0 ; i < DRM_RESERVED_CONTEXTS ; i++ ) {
243 if ( DRM_COPY_TO_USER( &res.contexts[i],
244 &ctx, sizeof(ctx) ) )
245 return DRM_ERR(EFAULT);
248 res.count = DRM_RESERVED_CONTEXTS;
250 DRM_COPY_TO_USER_IOCTL( (drm_ctx_res_t *)data, res, sizeof(res) );
255 int drm_addctx(DRM_IOCTL_ARGS)
260 DRM_COPY_FROM_USER_IOCTL( ctx, (drm_ctx_t *)data, sizeof(ctx) );
262 ctx.handle = drm_ctxbitmap_next(dev);
263 if ( ctx.handle == DRM_KERNEL_CONTEXT ) {
264 /* Skip kernel's context and get a new one. */
265 ctx.handle = drm_ctxbitmap_next(dev);
267 DRM_DEBUG( "%d\n", ctx.handle );
268 if ( ctx.handle == -1 ) {
269 DRM_DEBUG( "Not enough free contexts.\n" );
270 /* Should this return -EBUSY instead? */
271 return DRM_ERR(ENOMEM);
274 if (dev->driver.context_ctor && ctx.handle != DRM_KERNEL_CONTEXT) {
276 dev->driver.context_ctor(dev, ctx.handle);
280 DRM_COPY_TO_USER_IOCTL( (drm_ctx_t *)data, ctx, sizeof(ctx) );
285 int drm_modctx(DRM_IOCTL_ARGS)
287 /* This does nothing */
291 int drm_getctx(DRM_IOCTL_ARGS)
295 DRM_COPY_FROM_USER_IOCTL( ctx, (drm_ctx_t *)data, sizeof(ctx) );
297 /* This is 0, because we don't handle any context flags */
300 DRM_COPY_TO_USER_IOCTL( (drm_ctx_t *)data, ctx, sizeof(ctx) );
305 int drm_switchctx(DRM_IOCTL_ARGS)
310 DRM_COPY_FROM_USER_IOCTL( ctx, (drm_ctx_t *)data, sizeof(ctx) );
312 DRM_DEBUG( "%d\n", ctx.handle );
313 return drm_context_switch(dev, dev->last_context, ctx.handle);
316 int drm_newctx(DRM_IOCTL_ARGS)
321 DRM_COPY_FROM_USER_IOCTL( ctx, (drm_ctx_t *)data, sizeof(ctx) );
323 DRM_DEBUG( "%d\n", ctx.handle );
324 drm_context_switch_complete(dev, ctx.handle);
329 int drm_rmctx(DRM_IOCTL_ARGS)
334 DRM_COPY_FROM_USER_IOCTL( ctx, (drm_ctx_t *)data, sizeof(ctx) );
336 DRM_DEBUG( "%d\n", ctx.handle );
337 if ( ctx.handle != DRM_KERNEL_CONTEXT ) {
338 if (dev->driver.context_dtor) {
340 dev->driver.context_dtor(dev, ctx.handle);
344 drm_ctxbitmap_free(dev, ctx.handle);