1 /* drm_context.h -- IOCTLs for generic contexts -*- linux-c -*-
2 * Created: Fri Nov 24 18:31:37 2000 by gareth@valinux.com
4 * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
5 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
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:
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
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.
28 * Rickard E. (Rik) Faith <faith@valinux.com>
29 * Gareth Hughes <gareth@valinux.com>
34 #define __NO_VERSION__
35 #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 ) goto failed;
46 if ( !dev->ctx_bitmap ) goto failed;
48 if ( ctx_handle < DRM_MAX_CTXBITMAP ) {
50 clear_bit( ctx_handle, dev->ctx_bitmap );
51 dev->context_sareas[ctx_handle] = NULL;
56 DRM_ERROR( "Attempt to free invalid context handle: %d\n",
61 int DRM(ctxbitmap_next)( drm_device_t *dev )
65 if(!dev->ctx_bitmap) return -1;
68 bit = find_first_zero_bit( dev->ctx_bitmap, DRM_MAX_CTXBITMAP );
69 if ( bit < DRM_MAX_CTXBITMAP ) {
70 set_bit( bit, dev->ctx_bitmap );
71 DRM_DEBUG( "drm_ctxbitmap_next bit : %d\n", bit );
72 if((bit+1) > dev->max_context) {
73 dev->max_context = (bit+1);
74 if(dev->context_sareas) {
75 drm_map_t **ctx_sareas;
77 ctx_sareas = DRM(realloc)(dev->context_sareas,
78 (dev->max_context - 1) *
79 sizeof(*dev->context_sareas),
81 sizeof(*dev->context_sareas),
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 = DRM(alloc)(
94 sizeof(*dev->context_sareas),
96 if(!dev->context_sareas) {
97 clear_bit(bit, dev->ctx_bitmap);
101 dev->context_sareas[bit] = NULL;
111 int DRM(ctxbitmap_init)( drm_device_t *dev )
117 dev->ctx_bitmap = (unsigned long *) DRM(alloc)( PAGE_SIZE,
119 if ( dev->ctx_bitmap == NULL ) {
121 return DRM_OS_ERR(ENOMEM);
123 memset( (void *)dev->ctx_bitmap, 0, PAGE_SIZE );
124 dev->context_sareas = NULL;
125 dev->max_context = -1;
128 for ( i = 0 ; i < DRM_RESERVED_CONTEXTS ; i++ ) {
129 temp = DRM(ctxbitmap_next)( dev );
130 DRM_DEBUG( "drm_ctxbitmap_init : %d\n", temp );
136 void DRM(ctxbitmap_cleanup)( drm_device_t *dev )
139 if( dev->context_sareas ) DRM(free)( dev->context_sareas,
140 sizeof(*dev->context_sareas) *
143 DRM(free)( (void *)dev->ctx_bitmap, PAGE_SIZE, DRM_MEM_CTXBITMAP );
147 /* ================================================================
148 * Per Context SAREA Support
151 int DRM(getsareactx)( DRM_OS_IOCTL )
154 drm_ctx_priv_map_t request;
157 DRM_OS_KRNFROMUSR( request, (drm_ctx_priv_map_t *)data,
161 if (dev->max_context < 0 || request.ctx_id >= (unsigned) dev->max_context) {
163 return DRM_OS_ERR(EINVAL);
166 map = dev->context_sareas[request.ctx_id];
169 request.handle = map->handle;
171 DRM_OS_KRNTOUSR( (drm_ctx_priv_map_t *)data, request, sizeof(request) );
176 int DRM(setsareactx)( DRM_OS_IOCTL )
179 drm_ctx_priv_map_t request;
180 drm_map_t *map = NULL;
182 drm_map_list_t *r_list = NULL;
183 struct list_head *list;
184 #endif /* __linux__ */
186 drm_map_list_entry_t *list;
187 #endif /* __FreeBSD__ */
189 DRM_OS_KRNFROMUSR( request, (drm_ctx_priv_map_t *)data,
194 list_for_each(list, &dev->maplist->head) {
195 r_list = (drm_map_list_t *)list;
197 r_list->map->handle == request.handle)
200 #endif /* __linux__ */
202 TAILQ_FOREACH(list, dev->maplist, link) {
204 if(map->handle == request.handle)
207 #endif /* __FreeBSD__ */
211 return DRM_OS_ERR(EINVAL);
216 #endif /* __linux__ */
219 #endif /* __FreeBSD__ */
221 if (dev->max_context < 0)
223 if (request.ctx_id >= (unsigned) dev->max_context)
225 dev->context_sareas[request.ctx_id] = map;
230 /* ================================================================
231 * The actual DRM context handling routines
234 int DRM(context_switch)( drm_device_t *dev, int old, int new )
238 if ( test_and_set_bit( 0, &dev->context_flag ) ) {
239 DRM_ERROR( "Reentering -- FIXME\n" );
240 return DRM_OS_ERR(EBUSY);
243 #if __HAVE_DMA_HISTOGRAM
244 dev->ctx_start = get_cycles();
247 DRM_DEBUG( "Context switch from %d to %d\n", old, new );
249 if ( new == dev->last_context ) {
250 clear_bit( 0, &dev->context_flag );
254 if ( DRM(flags) & DRM_FLAG_NOCTX ) {
255 DRM(context_switch_complete)( dev, new );
257 sprintf( buf, "C %d %d\n", old, new );
258 DRM(write_string)( dev, buf );
264 int DRM(context_switch_complete)( drm_device_t *dev, int new )
266 dev->last_context = new; /* PRE/POST: This is the _only_ writer. */
267 dev->last_switch = jiffies;
269 if ( !_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) ) {
270 DRM_ERROR( "Lock isn't held after context switch\n" );
273 /* If a context switch is ever initiated
274 when the kernel holds the lock, release
276 #if __HAVE_DMA_HISTOGRAM
277 atomic_inc( &dev->histo.ctx[DRM(histogram_slot)(get_cycles()
278 - dev->ctx_start)] );
281 clear_bit( 0, &dev->context_flag );
282 DRM_OS_WAKEUP( &dev->context_wait );
287 int DRM(resctx)( DRM_OS_IOCTL )
293 DRM_OS_KRNFROMUSR( res, (drm_ctx_res_t *)data, sizeof(res) );
295 if ( res.count >= DRM_RESERVED_CONTEXTS ) {
296 memset( &ctx, 0, sizeof(ctx) );
297 for ( i = 0 ; i < DRM_RESERVED_CONTEXTS ; i++ ) {
299 if ( DRM_OS_COPYTOUSR( &res.contexts[i],
301 return DRM_OS_ERR(EFAULT);
304 res.count = DRM_RESERVED_CONTEXTS;
306 DRM_OS_KRNTOUSR( (drm_ctx_res_t *)data, res, sizeof(res) );
311 int DRM(addctx)( DRM_OS_IOCTL )
316 DRM_OS_KRNFROMUSR( ctx, (drm_ctx_t *)data, sizeof(ctx) );
318 ctx.handle = DRM(ctxbitmap_next)( dev );
319 if ( ctx.handle == DRM_KERNEL_CONTEXT ) {
320 /* Skip kernel's context and get a new one. */
321 ctx.handle = DRM(ctxbitmap_next)( dev );
323 DRM_DEBUG( "%d\n", ctx.handle );
324 if ( ctx.handle == -1 ) {
325 DRM_DEBUG( "Not enough free contexts.\n" );
326 /* Should this return -EBUSY instead? */
327 return DRM_OS_ERR(ENOMEM);
330 DRM_OS_KRNTOUSR( (drm_ctx_t *)data, ctx, sizeof(ctx) );
335 int DRM(modctx)( DRM_OS_IOCTL )
337 /* This does nothing */
341 int DRM(getctx)( DRM_OS_IOCTL )
345 DRM_OS_KRNFROMUSR( ctx, (drm_ctx_t *)data, sizeof(ctx) );
347 /* This is 0, because we don't handle any context flags */
350 DRM_OS_KRNTOUSR( (drm_ctx_t *)data, ctx, sizeof(ctx) );
355 int DRM(switchctx)( DRM_OS_IOCTL )
360 DRM_OS_KRNFROMUSR( ctx, (drm_ctx_t *)data, sizeof(ctx) );
362 DRM_DEBUG( "%d\n", ctx.handle );
363 return DRM(context_switch)( dev, dev->last_context, ctx.handle );
366 int DRM(newctx)( DRM_OS_IOCTL )
371 DRM_OS_KRNFROMUSR( ctx, (drm_ctx_t *)data, sizeof(ctx) );
373 DRM_DEBUG( "%d\n", ctx.handle );
374 DRM(context_switch_complete)( dev, ctx.handle );
379 int DRM(rmctx)( DRM_OS_IOCTL )
384 DRM_OS_KRNFROMUSR( ctx, (drm_ctx_t *)data, sizeof(ctx) );
386 DRM_DEBUG( "%d\n", ctx.handle );
388 if ( ctx.handle == DRM_KERNEL_CONTEXT + 1 ) {
389 priv->remove_auth_on_close = 1;
391 #endif /* __linux__ */
392 if ( ctx.handle != DRM_KERNEL_CONTEXT ) {
393 DRM(ctxbitmap_free)( dev, ctx.handle );
400 #else /* __HAVE_CTX_BITMAP */
402 /* ================================================================
403 * Old-style context support
407 int DRM(context_switch)(drm_device_t *dev, int old, int new)
413 atomic_inc(&dev->total_ctx);
416 if (test_and_set_bit(0, &dev->context_flag)) {
417 DRM_ERROR("Reentering -- FIXME\n");
418 return DRM_OS_ERR(EBUSY);
421 #if __HAVE_DMA_HISTOGRAM
422 dev->ctx_start = get_cycles();
425 DRM_DEBUG("Context switch from %d to %d\n", old, new);
427 if (new >= dev->queue_count) {
428 clear_bit(0, &dev->context_flag);
429 return DRM_OS_ERR(EINVAL);
432 if (new == dev->last_context) {
433 clear_bit(0, &dev->context_flag);
437 q = dev->queuelist[new];
438 atomic_inc(&q->use_count);
439 if (atomic_read(&q->use_count) == 1) {
440 atomic_dec(&q->use_count);
441 clear_bit(0, &dev->context_flag);
442 return DRM_OS_ERR(EINVAL);
445 if (DRM(flags) & DRM_FLAG_NOCTX) {
446 DRM(context_switch_complete)(dev, new);
448 sprintf(buf, "C %d %d\n", old, new);
449 DRM(write_string)(dev, buf);
452 atomic_dec(&q->use_count);
457 int DRM(context_switch_complete)(drm_device_t *dev, int new)
459 drm_device_dma_t *dma = dev->dma;
461 dev->last_context = new; /* PRE/POST: This is the _only_ writer. */
462 dev->last_switch = jiffies;
464 if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock)) {
465 DRM_ERROR("Lock isn't held after context switch\n");
468 if (!dma || !(dma->next_buffer && dma->next_buffer->while_locked)) {
469 if (DRM(lock_free)(dev, &dev->lock.hw_lock->lock,
470 DRM_KERNEL_CONTEXT)) {
471 DRM_ERROR("Cannot free lock\n");
475 #if __HAVE_DMA_HISTOGRAM
476 atomic_inc(&dev->histo.ctx[DRM(histogram_slot)(get_cycles()
480 clear_bit(0, &dev->context_flag);
481 DRM_OS_WAKEUP_INT(&dev->context_wait);
486 static int DRM(init_queue)(drm_device_t *dev, drm_queue_t *q, drm_ctx_t *ctx)
490 if (atomic_read(&q->use_count) != 1
491 || atomic_read(&q->finalization)
492 || atomic_read(&q->block_count)) {
493 DRM_ERROR("New queue is already in use: u%ld f%ld b%ld\n",
494 (unsigned long)atomic_read(&q->use_count),
495 (unsigned long)atomic_read(&q->finalization),
496 (unsigned long)atomic_read(&q->block_count));
499 atomic_set(&q->finalization, 0);
500 atomic_set(&q->block_count, 0);
501 atomic_set(&q->block_read, 0);
502 atomic_set(&q->block_write, 0);
503 atomic_set(&q->total_queued, 0);
504 atomic_set(&q->total_flushed, 0);
505 atomic_set(&q->total_locks, 0);
508 init_waitqueue_head(&q->write_queue);
509 init_waitqueue_head(&q->read_queue);
510 init_waitqueue_head(&q->flush_queue);
511 #endif /* __linux__ */
516 #endif /* __FreeBSD__ */
518 q->flags = ctx->flags;
520 DRM(waitlist_create)(&q->waitlist, dev->dma->buf_count);
527 PRE: 1) dev->queuelist[0..dev->queue_count] is allocated and will not
528 disappear (so all deallocation must be done after IOCTLs are off)
529 2) dev->queue_count < dev->queue_slots
530 3) dev->queuelist[i].use_count == 0 and
531 dev->queuelist[i].finalization == 0 if i not in use
532 POST: 1) dev->queuelist[i].use_count == 1
533 2) dev->queue_count < dev->queue_slots */
535 static int DRM(alloc_queue)(drm_device_t *dev)
541 /* Check for a free queue */
542 for (i = 0; i < dev->queue_count; i++) {
543 atomic_inc(&dev->queuelist[i]->use_count);
544 if (atomic_read(&dev->queuelist[i]->use_count) == 1
545 && !atomic_read(&dev->queuelist[i]->finalization)) {
546 DRM_DEBUG("%d (free)\n", i);
549 atomic_dec(&dev->queuelist[i]->use_count);
551 /* Allocate a new queue */
554 queue = gamma_alloc(sizeof(*queue), DRM_MEM_QUEUES);
555 memset(queue, 0, sizeof(*queue));
556 atomic_set(&queue->use_count, 1);
559 if (dev->queue_count >= dev->queue_slots) {
560 oldslots = dev->queue_slots * sizeof(*dev->queuelist);
561 if (!dev->queue_slots) dev->queue_slots = 1;
562 dev->queue_slots *= 2;
563 newslots = dev->queue_slots * sizeof(*dev->queuelist);
565 dev->queuelist = DRM(realloc)(dev->queuelist,
569 if (!dev->queuelist) {
571 DRM_DEBUG("out of memory\n");
572 return DRM_OS_ERR(ENOMEM);
575 dev->queuelist[dev->queue_count-1] = queue;
578 DRM_DEBUG("%d (new)\n", dev->queue_count - 1);
579 return dev->queue_count - 1;
582 int DRM(resctx)( DRM_OS_IOCTL )
588 DRM_DEBUG("%d\n", DRM_RESERVED_CONTEXTS);
590 DRM_OS_KRNFROMUSR( res, (drm_ctx_res_t *)data, sizeof(res) );
592 if (res.count >= DRM_RESERVED_CONTEXTS) {
593 memset(&ctx, 0, sizeof(ctx));
594 for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
596 if (DRM_OS_COPYTOUSR(&res.contexts[i],
599 return DRM_OS_ERR(EFAULT);
602 res.count = DRM_RESERVED_CONTEXTS;
604 DRM_OS_KRNTOUSR( (drm_ctx_res_t *)data, res, sizeof(res) );
609 int DRM(addctx)( DRM_OS_IOCTL )
614 DRM_OS_KRNFROMUSR( ctx, (drm_ctx_t *)data, sizeof(ctx) );
616 if ((ctx.handle = DRM(alloc_queue)(dev)) == DRM_KERNEL_CONTEXT) {
617 /* Init kernel's context and get a new one. */
618 DRM(init_queue)(dev, dev->queuelist[ctx.handle], &ctx);
619 ctx.handle = DRM(alloc_queue)(dev);
621 DRM(init_queue)(dev, dev->queuelist[ctx.handle], &ctx);
622 DRM_DEBUG("%d\n", ctx.handle);
624 DRM_OS_KRNTOUSR( (drm_ctx_t *)data, ctx, sizeof(ctx) );
629 int DRM(modctx)( DRM_OS_IOCTL )
635 DRM_OS_KRNFROMUSR( ctx, (drm_ctx_t *)data, sizeof(ctx) );
637 DRM_DEBUG("%d\n", ctx.handle);
639 if (ctx.handle < 0 || ctx.handle >= dev->queue_count)
640 return DRM_OS_ERR(EINVAL);
641 q = dev->queuelist[ctx.handle];
643 atomic_inc(&q->use_count);
644 if (atomic_read(&q->use_count) == 1) {
645 /* No longer in use */
646 atomic_dec(&q->use_count);
647 return DRM_OS_ERR(EINVAL);
650 if (DRM_BUFCOUNT(&q->waitlist)) {
651 atomic_dec(&q->use_count);
652 return DRM_OS_ERR(EBUSY);
655 q->flags = ctx.flags;
657 atomic_dec(&q->use_count);
661 int DRM(getctx)( DRM_OS_IOCTL )
667 DRM_OS_KRNFROMUSR( ctx, (drm_ctx_t *)data, sizeof(ctx) );
669 DRM_DEBUG("%d\n", ctx.handle);
671 if (ctx.handle >= dev->queue_count)
672 return DRM_OS_ERR(EINVAL);
673 q = dev->queuelist[ctx.handle];
675 atomic_inc(&q->use_count);
676 if (atomic_read(&q->use_count) == 1) {
677 /* No longer in use */
678 atomic_dec(&q->use_count);
679 return DRM_OS_ERR(EINVAL);
682 ctx.flags = q->flags;
683 atomic_dec(&q->use_count);
685 DRM_OS_KRNTOUSR( (drm_ctx_t *)data, ctx, sizeof(ctx) );
690 int DRM(switchctx)( DRM_OS_IOCTL )
695 DRM_OS_KRNFROMUSR( ctx, (drm_ctx_t *)data, sizeof(ctx) );
697 DRM_DEBUG("%d\n", ctx.handle);
698 return DRM(context_switch)(dev, dev->last_context, ctx.handle);
701 int DRM(newctx)( DRM_OS_IOCTL )
706 DRM_OS_KRNFROMUSR( ctx, (drm_ctx_t *)data, sizeof(ctx) );
708 DRM_DEBUG("%d\n", ctx.handle);
709 DRM(context_switch_complete)(dev, ctx.handle);
714 int DRM(rmctx)( DRM_OS_IOCTL )
721 DRM_OS_KRNFROMUSR( ctx, (drm_ctx_t *)data, sizeof(ctx) );
723 DRM_DEBUG("%d\n", ctx.handle);
725 if (ctx.handle >= dev->queue_count) return DRM_OS_ERR(EINVAL);
726 q = dev->queuelist[ctx.handle];
728 atomic_inc(&q->use_count);
729 if (atomic_read(&q->use_count) == 1) {
730 /* No longer in use */
731 atomic_dec(&q->use_count);
732 return DRM_OS_ERR(EINVAL);
735 atomic_inc(&q->finalization); /* Mark queue in finalization state */
736 atomic_sub(2, &q->use_count); /* Mark queue as unused (pending
739 while (test_and_set_bit(0, &dev->interrupt_flag)) {
742 if (signal_pending(current)) {
743 clear_bit(0, &dev->interrupt_flag);
744 return DRM_OS_ERR(EINTR);
746 #endif /* __linux__ */
750 retcode = tsleep(&never, PZERO|PCATCH, "never", 1);
753 #endif /* __FreeBSD__ */
755 /* Remove queued buffers */
756 while ((buf = DRM(waitlist_get)(&q->waitlist))) {
757 DRM(free_buffer)(dev, buf);
759 clear_bit(0, &dev->interrupt_flag);
761 /* Wakeup blocked processes */
763 wake_up_interruptible(&q->read_queue);
764 wake_up_interruptible(&q->write_queue);
765 #endif /* __linux__ */
767 wakeup( &q->block_read );
768 wakeup( &q->block_write );
769 #endif /* __FreeBSD__ */
770 DRM_OS_WAKEUP_INT( &q->flush_queue );
771 /* Finalization over. Queue is made
772 available when both use_count and
773 finalization become 0, which won't
774 happen until all the waiting processes
776 atomic_dec(&q->finalization);
780 #endif /* __HAVE_CTX_BITMAP */