summaryrefslogtreecommitdiff
AgeCommit message (Expand)Author
2008-05-12drm: remove root only from a lot of drm ioctls to get stuff running as non-rootDave Airlie
2008-05-12drm: masters are always authenticatedDave Airlie
2008-05-12drm: fix oops on reading proc file with no masterDave Airlie
2008-05-11[GEM] Make pread/pwrite manage memory domains. No luck with movnti though.Keith Packard
2008-05-10[intel-GEM] exec list can contain pinned, lru cannot.Keith Packard
2008-05-10Merge commit 'anholt/drm-gem' into drm-gemKeith Packard
2008-05-10[GEM] Add drm-gem.txtKeith Packard
2008-05-10[intel-GEM] Clean up GEM ioctl naming.Keith Packard
2008-05-09GEM: Fix arguments to drm_memrange_init so we don't exceed our allocation.Eric Anholt
2008-05-09GEM: Separate the LRU into execution list and LRU list.Eric Anholt
2008-05-09GEM: Clear obj_priv->agp_mem when we free it.Eric Anholt
2008-05-09GEM: Avoid leaking refs on target objects on presumed offset success.Eric Anholt
2008-05-09fixup i915 workqueue handling when modeset=1Hong Liu
2008-05-09i915: use BDB TV flag for TV detectionJesse Barnes
2008-05-09i915: add basic VBT supportJesse Barnes
2008-05-09[gem] API cleanup. allocate->create unreference->close name->flinkKeith Packard
2008-05-09Fix build problemsAlan Hourihane
2008-05-09Fix test applications for recent DRM changesAlan Hourihane
2008-05-08[i915] clean up whinging from checkpatch.plKeith Packard
2008-05-08Clean up whinging from checkpatch.pl in drm_gem.cKeith Packard
2008-05-08GEM: Fix oops on NULL dereference when we try clflushing when we don't need to.Eric Anholt
2008-05-08i915: Changed intel_fb to use the new drm_crtc_set_config interfaceJakob Bornecrantz
2008-05-08[intel-gem] Move domains to relocation records. add set_domain ioctl.Keith Packard
2008-05-08i915: Fixed indent in intel_fb.cJakob Bornecrantz
2008-05-08drm: Made set_config use drm_mode_set as a argumentJakob Bornecrantz
2008-05-08Revert "i915: fix vbl swap for multi-master"Dave Airlie
2008-05-08i915: fix vbl swap for multi-masterDave Airlie
2008-05-08drm: check for NULL fb here, shouldn't happen but avoid oops for nowDave Airlie
2008-05-08drm: set crtc->fb to NULLDave Airlie
2008-05-08intel: set correct limits on screen width/height from DDXDave Airlie
2008-05-08drm: fix replacefb to change fb propertiesDave Airlie
2008-05-08drm_mode: initial replacefb implemenationDave Airlie
2008-05-08drm: fix typo from previous mergeDave Airlie
2008-05-08cursor: pass handle not BO.Dave Airlie
2008-05-08Merge remote branch 'origin/master' into modesetting-101Dave Airlie
2008-05-07GEM: fix testcases for new ioctl args.Eric Anholt
2008-05-07Apply a few stylistic cleanups to match kernel code.Arjan van de Ven
2008-05-07GEM: Wait for existing rendering to complete before writing relocation data.Eric Anholt
2008-05-07GEM: Extend cache domain stuff for 965.Eric Anholt
2008-05-07drm: nopage compat fixup for drm_vmDave Airlie
2008-05-06[intel-GEM] ref count objects in gtt-lru.Keith Packard
2008-05-06[intel-GEM] Add memory domain support.Keith Packard
2008-05-06Merge commit 'anholt/drm-gem' into drm-gemKeith Packard
2008-05-06Start coding up memory domainsKeith Packard
2008-05-06GEM: Use irq-based fencing rather than syncing and evicting every exec.Eric Anholt
2008-05-06GEM: Skip relocation if presumed offset matches.Eric Anholt
2008-05-06GEM: Save the last ioremapped page for relocations in case we need it again.Eric Anholt
2008-05-05Dump last batch buffer when hardware lockup is detected.Keith Packard
2008-05-05Monitor ACTHD register while polling for idle ring.Keith Packard
2008-05-05Unlock pages right after getting them.Keith Packard
382'>382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
/* sis.c -- sis driver -*- linux-c -*-
 * Created: Thu Oct  7 10:38:32 1999 by faith@precisioninsight.com
 *
 * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 * Authors:
 *    Rickard E. (Rik) Faith <faith@precisioninsight.com>
 *    Daryll Strauss <daryll@precisioninsight.com>
 *
 */

#include <linux/config.h>
#include "drmP.h"
#include "sis_drm_public.h"
#include "sis_drv.h"

#define SIS_NAME	 "sis"
#define SIS_DESC	 "sis"
#define SIS_DATE	 "19991009"
#define SIS_MAJOR	 0
#define SIS_MINOR	 0
#define SIS_PATCHLEVEL  1

static drm_device_t	      sis_device;
drm_ctx_t	              sis_res_ctx;

static struct file_operations sis_fops = {
	open:	 sis_open,
	flush:	 drm_flush,
	release: sis_release,
	ioctl:	 sis_ioctl,
	mmap:	 drm_mmap,
	read:	 drm_read,
	fasync:	 drm_fasync,
	poll:	 drm_poll,
};

static struct miscdevice      sis_misc = {
	minor: MISC_DYNAMIC_MINOR,
	name:  SIS_NAME,
	fops:  &sis_fops,
};

static drm_ioctl_desc_t	      sis_ioctls[] = {
	[DRM_IOCTL_NR(DRM_IOCTL_VERSION)]    = { sis_version,	  0, 0 },
	[DRM_IOCTL_NR(DRM_IOCTL_GET_UNIQUE)] = { drm_getunique,	  0, 0 },
	[DRM_IOCTL_NR(DRM_IOCTL_GET_MAGIC)]  = { drm_getmagic,	  0, 0 },
	[DRM_IOCTL_NR(DRM_IOCTL_IRQ_BUSID)]  = { drm_irq_busid,	  0, 1 },

	[DRM_IOCTL_NR(DRM_IOCTL_SET_UNIQUE)] = { drm_setunique,	  1, 1 },
	[DRM_IOCTL_NR(DRM_IOCTL_BLOCK)]	     = { drm_block,	  1, 1 },
	[DRM_IOCTL_NR(DRM_IOCTL_UNBLOCK)]    = { drm_unblock,	  1, 1 },

	[DRM_IOCTL_NR(DRM_IOCTL_AUTH_MAGIC)] = { drm_authmagic,	  1, 1 },
	[DRM_IOCTL_NR(DRM_IOCTL_ADD_MAP)]    = { drm_addmap,	  1, 1 },
	[DRM_IOCTL_NR(DRM_IOCTL_ADD_CTX)]    = { sis_addctx,	  1, 1 },
	[DRM_IOCTL_NR(DRM_IOCTL_RM_CTX)]     = { sis_rmctx,	  1, 1 },
	[DRM_IOCTL_NR(DRM_IOCTL_MOD_CTX)]    = { sis_modctx,	  1, 1 },
	[DRM_IOCTL_NR(DRM_IOCTL_GET_CTX)]    = { sis_getctx,	  1, 0 },
	[DRM_IOCTL_NR(DRM_IOCTL_SWITCH_CTX)] = { sis_switchctx,   1, 1 },
	[DRM_IOCTL_NR(DRM_IOCTL_NEW_CTX)]    = { sis_newctx,	  1, 1 },
	[DRM_IOCTL_NR(DRM_IOCTL_RES_CTX)]    = { sis_resctx,	  1, 0 },
	[DRM_IOCTL_NR(DRM_IOCTL_ADD_DRAW)]   = { drm_adddraw,	  1, 1 },
	[DRM_IOCTL_NR(DRM_IOCTL_RM_DRAW)]    = { drm_rmdraw,	  1, 1 },
	[DRM_IOCTL_NR(DRM_IOCTL_LOCK)]	     = { sis_lock,	  1, 0 },
	[DRM_IOCTL_NR(DRM_IOCTL_UNLOCK)]     = { sis_unlock,	  1, 0 },
	[DRM_IOCTL_NR(DRM_IOCTL_FINISH)]     = { drm_finish,	  1, 0 },

#ifdef DRM_AGP
	[DRM_IOCTL_NR(DRM_IOCTL_AGP_ACQUIRE)] = { drm_agp_acquire, 1, 1 },
	[DRM_IOCTL_NR(DRM_IOCTL_AGP_RELEASE)] = { drm_agp_release, 1, 1 },
	[DRM_IOCTL_NR(DRM_IOCTL_AGP_ENABLE)]  = { drm_agp_enable,  1, 1 },
	[DRM_IOCTL_NR(DRM_IOCTL_AGP_INFO)]    = { drm_agp_info,    1, 0 },
	[DRM_IOCTL_NR(DRM_IOCTL_AGP_ALLOC)]   = { drm_agp_alloc,   1, 1 },
	[DRM_IOCTL_NR(DRM_IOCTL_AGP_FREE)]    = { drm_agp_free,    1, 1 },
	[DRM_IOCTL_NR(DRM_IOCTL_AGP_BIND)]    = { drm_agp_bind,    1, 1 },
	[DRM_IOCTL_NR(DRM_IOCTL_AGP_UNBIND)]  = { drm_agp_unbind,  1, 1 },
#endif

        /* FB Memory Management */
        [DRM_IOCTL_NR(SIS_IOCTL_FB_ALLOC)]   = { sis_fb_alloc,	  1, 1 },
        [DRM_IOCTL_NR(SIS_IOCTL_FB_FREE)]    = { sis_fb_free,	  1, 1 },

        /* AGP Memory Management */
        [DRM_IOCTL_NR(SIS_IOCTL_AGP_INIT)]   = { sis_agp_init,	  1, 1 },
        [DRM_IOCTL_NR(SIS_IOCTL_AGP_ALLOC)]  = { sis_agp_alloc,	  1, 1 },
        [DRM_IOCTL_NR(SIS_IOCTL_AGP_FREE)]   = { sis_agp_free,	  1, 1 },
        
#if defined(SIS_STEREO)
	[DRM_IOCTL_NR(DRM_IOCTL_CONTROL)]    = { sis_control,	  1, 1 },
        [DRM_IOCTL_NR(SIS_IOCTL_FLIP)]       = { sis_flip,	  1, 1 },
        [DRM_IOCTL_NR(SIS_IOCTL_FLIP_INIT)]  = { sis_flip_init,	  1, 1 },
        [DRM_IOCTL_NR(SIS_IOCTL_FLIP_FINAL)] = { sis_flip_final,  1, 1 },
#endif
};
#define SIS_IOCTL_COUNT DRM_ARRAY_SIZE(sis_ioctls)

#ifdef MODULE
static char		      *sis = NULL;
#endif

MODULE_AUTHOR("Precision Insight, Inc., Cedar Park, Texas.");
MODULE_DESCRIPTION("sis");
MODULE_PARM(sis, "s");

static int sis_setup(drm_device_t *dev)
{
	int i;
	
	atomic_set(&dev->ioctl_count, 0);
	atomic_set(&dev->vma_count, 0);
	dev->buf_use	  = 0;
	atomic_set(&dev->buf_alloc, 0);

	atomic_set(&dev->total_open, 0);
	atomic_set(&dev->total_close, 0);
	atomic_set(&dev->total_ioctl, 0);
	atomic_set(&dev->total_irq, 0);
	atomic_set(&dev->total_ctx, 0);
	atomic_set(&dev->total_locks, 0);
	atomic_set(&dev->total_unlocks, 0);
	atomic_set(&dev->total_contends, 0);
	atomic_set(&dev->total_sleeps, 0);

	for (i = 0; i < DRM_HASH_SIZE; i++) {
		dev->magiclist[i].head = NULL;
		dev->magiclist[i].tail = NULL;
	}
	dev->maplist	    = NULL;
	dev->map_count	    = 0;
	dev->vmalist	    = NULL;
	dev->lock.hw_lock   = NULL;
	init_waitqueue_head(&dev->lock.lock_queue);
	dev->queue_count    = 0;
	dev->queue_reserved = 0;
	dev->queue_slots    = 0;
	dev->queuelist	    = NULL;
	dev->irq	    = 0;
	dev->context_flag   = 0;
	dev->interrupt_flag = 0;
	dev->dma            = 0;
	dev->dma_flag	    = 0;
	dev->last_context   = 0;
	dev->last_switch    = 0;
	dev->last_checked   = 0;
	init_timer(&dev->timer);
	init_waitqueue_head(&dev->context_wait);

	dev->ctx_start	    = 0;
	dev->lck_start	    = 0;
	
	dev->buf_rp	  = dev->buf;
	dev->buf_wp	  = dev->buf;
	dev->buf_end	  = dev->buf + DRM_BSZ;
	dev->buf_async	  = NULL;
	init_waitqueue_head(&dev->buf_readers);
	init_waitqueue_head(&dev->buf_writers);

	sis_res_ctx.handle=-1;
			
	DRM_DEBUG("\n");
			
	/* The kernel's context could be created here, but is now created
	   in drm_dma_enqueue.	This is more resource-efficient for
	   hardware that does not do DMA, but may mean that
	   drm_select_queue fails between the time the interrupt is
	   initialized and the time the queues are initialized. */
			
	return 0;
}


static int sis_takedown(drm_device_t *dev)
{
	int		  i;
	drm_magic_entry_t *pt, *next;
	drm_map_t	  *map;
	drm_vma_entry_t	  *vma, *vma_next;

	DRM_DEBUG("\n");

#if defined(SIS_STEREO)
	if (dev->irq) sis_irq_uninstall(dev);
#endif

	down(&dev->struct_sem);
	del_timer(&dev->timer);
	
	if (dev->devname) {
		drm_free(dev->devname, strlen(dev->devname)+1, DRM_MEM_DRIVER);
		dev->devname = NULL;
	}
	
	if (dev->unique) {
		drm_free(dev->unique, strlen(dev->unique)+1, DRM_MEM_DRIVER);
		dev->unique = NULL;
		dev->unique_len = 0;
	}
				/* Clear pid list */
	for (i = 0; i < DRM_HASH_SIZE; i++) {
		for (pt = dev->magiclist[i].head; pt; pt = next) {
			next = pt->next;
			drm_free(pt, sizeof(*pt), DRM_MEM_MAGIC);
		}
		dev->magiclist[i].head = dev->magiclist[i].tail = NULL;
	}
#ifdef DRM_AGP
   				/* Clear AGP information */
	if (dev->agp) {
		drm_agp_mem_t *entry;
		drm_agp_mem_t *nexte;
		
				/* Remove AGP resources, but leave dev->agp
                                   intact until cleanup is called. */
		for (entry = dev->agp->memory; entry; entry = nexte) {
			nexte = entry->next;
			if (entry->bound) drm_unbind_agp(entry->memory);
			drm_free_agp(entry->memory, entry->pages);
			drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
		}
		dev->agp->memory = NULL;
		
		if (dev->agp->acquired && drm_agp.release)
			(*drm_agp.release)();
		
		dev->agp->acquired = 0;
		dev->agp->enabled  = 0;
	}	
#endif

				/* Clear vma list (only built for debugging) */
	if (dev->vmalist) {
		for (vma = dev->vmalist; vma; vma = vma_next) {
			vma_next = vma->next;
			drm_free(vma, sizeof(*vma), DRM_MEM_VMAS);
		}
		dev->vmalist = NULL;
	}
	
				/* Clear map area and mtrr information */
	if (dev->maplist) {
		for (i = 0; i < dev->map_count; i++) {
			map = dev->maplist[i];
			switch (map->type) {
			case _DRM_REGISTERS:
			case _DRM_FRAME_BUFFER:
#ifdef CONFIG_MTRR
				if (map->mtrr >= 0) {
					int retcode;
					retcode = mtrr_del(map->mtrr,
							   map->offset,
							   map->size);
					DRM_DEBUG("mtrr_del = %d\n", retcode);
				}
#endif
				drm_ioremapfree(map->handle, map->size);
				break;
			case _DRM_SHM:
				drm_free_pages((unsigned long)map->handle,
					       drm_order(map->size)
					       - PAGE_SHIFT,
					       DRM_MEM_SAREA);
				break;
			case _DRM_AGP:
				/* Do nothing here, because this is all
                                   handled in the AGP/GART driver. */
				break;
			}
			drm_free(map, sizeof(*map), DRM_MEM_MAPS);
		}
		drm_free(dev->maplist,
			 dev->map_count * sizeof(*dev->maplist),
			 DRM_MEM_MAPS);
		dev->maplist   = NULL;
		dev->map_count = 0;
	}
	
	if (dev->lock.hw_lock) {
		dev->lock.hw_lock    = NULL; /* SHM removed */
		dev->lock.pid	     = 0;
		wake_up_interruptible(&dev->lock.lock_queue);
	}
	up(&dev->struct_sem);
	
	return 0;
}

/* sis_init is called via init_module at module load time, or via
 * linux/init/main.c (this is not currently supported). */

int sis_init(void)
{
	int		      retcode;
	drm_device_t	      *dev = &sis_device;

	DRM_DEBUG("\n");

	memset((void *)dev, 0, sizeof(*dev));
	dev->count_lock	  = SPIN_LOCK_UNLOCKED;
	sema_init(&dev->struct_sem, 1);
	
#ifdef MODULE
	drm_parse_options(sis);
#endif

	if ((retcode = misc_register(&sis_misc))) {
		DRM_ERROR("Cannot register \"%s\"\n", SIS_NAME);
		return retcode;
	}
	dev->device = MKDEV(MISC_MAJOR, sis_misc.minor);
	dev->name   = SIS_NAME;

	drm_mem_init();
	drm_proc_init(dev);

#ifdef DRM_AGP
	DRM_DEBUG("doing agp init\n");
	dev->agp    = drm_agp_init();
      	if(dev->agp == NULL) {
	   	/* TODO, if no agp, run MMIO mode */
	   	DRM_INFO("The sis drm module requires the agpgart module"
		         " to function correctly\nPlease load the agpgart"
		         " module before you load the mga module\n");
	   	drm_proc_cleanup();
	   	misc_deregister(&sis_misc);
	   	sis_takedown(dev);
	   	return -ENOMEM;
	}
#ifdef CONFIG_MTRR
   	dev->agp->agp_mtrr = mtrr_add(dev->agp->agp_info.aper_base,
				      dev->agp->agp_info.aper_size * 1024 * 1024,
				      MTRR_TYPE_WRCOMB,
				      1);
#endif
#endif

	DRM_INFO("Initialized %s %d.%d.%d %s on minor %d\n",
		 SIS_NAME,
		 SIS_MAJOR,
		 SIS_MINOR,
		 SIS_PATCHLEVEL,
		 SIS_DATE,
		 sis_misc.minor);
	
	return 0;
}

/* sis_cleanup is called via cleanup_module at module unload time. */

void sis_cleanup(void)
{
	drm_device_t	      *dev = &sis_device;

	DRM_DEBUG("\n");
	
	drm_proc_cleanup();
	if (misc_deregister(&sis_misc)) {
		DRM_ERROR("Cannot unload module\n");
	} else {
		DRM_INFO("Module unloaded\n");
	}
#ifdef DRM_AGP
#ifdef CONFIG_MTRR
   	if(dev->agp && dev->agp->agp_mtrr) {
	   	int retval;
	   	retval = mtrr_del(dev->agp->agp_mtrr, 
				  dev->agp->agp_info.aper_base,
				  dev->agp->agp_info.aper_size * 1024*1024);
	   	DRM_DEBUG("mtrr_del = %d\n", retval);
	}
#endif
#endif

	sis_takedown(dev);
#ifdef DRM_AGP
	if (dev->agp) {
		drm_free(dev->agp, sizeof(*dev->agp), DRM_MEM_AGPLISTS);
		dev->agp = NULL;
	}
#endif
}

int sis_version(struct inode *inode, struct file *filp, unsigned int cmd,
		  unsigned long arg)
{
	drm_version_t version;
	int	      len;

	copy_from_user_ret(&version,
			   (drm_version_t *)arg,
			   sizeof(version),
			   -EFAULT);

#define DRM_COPY(name,value)				     \
	len = strlen(value);				     \
	if (len > name##_len) len = name##_len;		     \
	name##_len = strlen(value);			     \
	if (len && name) {				     \
		copy_to_user_ret(name, value, len, -EFAULT); \
	}

	version.version_major	   = SIS_MAJOR;
	version.version_minor	   = SIS_MINOR;
	version.version_patchlevel = SIS_PATCHLEVEL;

	DRM_COPY(version.name, SIS_NAME);
	DRM_COPY(version.date, SIS_DATE);
	DRM_COPY(version.desc, SIS_DESC);

	copy_to_user_ret((drm_version_t *)arg,
			 &version,
			 sizeof(version),
			 -EFAULT);
	return 0;
}

int sis_open(struct inode *inode, struct file *filp)
{
	drm_device_t  *dev    = &sis_device;
	int	      retcode = 0;
	       
	DRM_DEBUG("open_count = %d\n", dev->open_count);

	if (!(retcode = drm_open_helper(inode, filp, dev))) {
		MOD_INC_USE_COUNT;
		atomic_inc(&dev->total_open);
		spin_lock(&dev->count_lock);
		if (!dev->open_count++) {
			spin_unlock(&dev->count_lock);
			return sis_setup(dev);
		}
		spin_unlock(&dev->count_lock);
	}
	return retcode;
}

int sis_release(struct inode *inode, struct file *filp)
{
	drm_file_t    *priv   = filp->private_data;
	drm_device_t  *dev    = priv->dev;
	int	      retcode = 0;

	DRM_DEBUG("open_count = %d\n", dev->open_count);

	if (!(retcode = drm_release(inode, filp))) {
		MOD_DEC_USE_COUNT;
		atomic_inc(&dev->total_close);
		spin_lock(&dev->count_lock);
		if (!--dev->open_count) {
			if (atomic_read(&dev->ioctl_count) || dev->blocked) {
				DRM_ERROR("Device busy: %d %d\n",
					  atomic_read(&dev->ioctl_count),
					  dev->blocked);
				spin_unlock(&dev->count_lock);
				return -EBUSY;
			}
			spin_unlock(&dev->count_lock);
			return sis_takedown(dev);
		}
		spin_unlock(&dev->count_lock);
	}
	return retcode;
}

/* sis_ioctl is called whenever a process performs an ioctl on /dev/drm. */

int sis_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
		unsigned long arg)
{
	int		 nr	 = DRM_IOCTL_NR(cmd);
	drm_file_t	 *priv	 = filp->private_data;
	drm_device_t	 *dev	 = priv->dev;
	int		 retcode = 0;
	drm_ioctl_desc_t *ioctl;
	drm_ioctl_t	 *func;

	atomic_inc(&dev->ioctl_count);
	atomic_inc(&dev->total_ioctl);
	++priv->ioctl_count;
	
	DRM_DEBUG("pid = %d, cmd = 0x%02x, nr = 0x%02x, dev 0x%x, auth = %d\n",
		  current->pid, cmd, nr, dev->device, priv->authenticated);

	if (nr >= SIS_IOCTL_COUNT) {
		retcode = -EINVAL;
	} else {
		ioctl	  = &sis_ioctls[nr];
		func	  = ioctl->func;

		if (!func) {
			DRM_DEBUG("no function\n");
			retcode = -EINVAL;
		} else if ((ioctl->root_only && !capable(CAP_SYS_ADMIN))
			    || (ioctl->auth_needed && !priv->authenticated)) {
			retcode = -EACCES;
		} else {
			retcode = (func)(inode, filp, cmd, arg);
		}
	}
	
	atomic_dec(&dev->ioctl_count);
	return retcode;
}

int sis_lock(struct inode *inode, struct file *filp, unsigned int cmd,
	      unsigned long arg)
{
        drm_file_t        *priv   = filp->private_data;
        drm_device_t      *dev    = priv->dev;
        DECLARE_WAITQUEUE(entry, current);
        int               ret   = 0;
        drm_lock_t        lock;
#if DRM_DMA_HISTOGRAM
        cycles_t          start;

        dev->lck_start = start = get_cycles();
#endif

        copy_from_user_ret(&lock, (drm_lock_t *)arg, sizeof(lock), -EFAULT);

        if (lock.context == DRM_KERNEL_CONTEXT) {
                DRM_ERROR("Process %d using kernel context %d\n",
                          current->pid, lock.context);
                return -EINVAL;
        }

        DRM_DEBUG("%d (pid %d) requests lock (0x%08x), flags = 0x%08x\n",
                  lock.context, current->pid, dev->lock.hw_lock->lock,
                  lock.flags);

#if 0
				/* dev->queue_count == 0 right now for
                                   sis.  FIXME? */
        if (lock.context < 0 || lock.context >= dev->queue_count)
                return -EINVAL;
#endif
        
        if (!ret) {
#if 0
                if (_DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock)
                    != lock.context) {
                        long j = jiffies - dev->lock.lock_time;

                        if (lock.context == sis_res_ctx.handle &&
				j >= 0 && j < DRM_LOCK_SLICE) {
                                /* Can't take lock if we just had it and
                                   there is contention. */
                                DRM_DEBUG("%d (pid %d) delayed j=%d dev=%d jiffies=%d\n",
					lock.context, current->pid, j, 
					dev->lock.lock_time, jiffies);
                                current->state = TASK_INTERRUPTIBLE;
				current->policy |= SCHED_YIELD;
                                schedule_timeout(DRM_LOCK_SLICE-j);
				DRM_DEBUG("jiffies=%d\n", jiffies);
                        }
                }
#endif
                add_wait_queue(&dev->lock.lock_queue, &entry);
                for (;;) {
                        if (!dev->lock.hw_lock) {
                                /* Device has been unregistered */
                                ret = -EINTR;
                                break;
                        }
                        if (drm_lock_take(&dev->lock.hw_lock->lock,
                                          lock.context)) {
                                dev->lock.pid       = current->pid;
                                dev->lock.lock_time = jiffies;
                                atomic_inc(&dev->total_locks);
                                break;  /* Got lock */
                        }
                        
                                /* Contention */
                        atomic_inc(&dev->total_sleeps);
                        current->state = TASK_INTERRUPTIBLE;
			current->policy |= SCHED_YIELD;
                        schedule();
                        if (signal_pending(current)) {
                                ret = -ERESTARTSYS;
                                break;
                        }
                }
                current->state = TASK_RUNNING;
                remove_wait_queue(&dev->lock.lock_queue, &entry);
        }

#if 0
	if (!ret && dev->last_context != lock.context &&
		lock.context != sis_res_ctx.handle &&
		dev->last_context != sis_res_ctx.handle) {
		add_wait_queue(&dev->context_wait, &entry);
	        current->state = TASK_INTERRUPTIBLE;
                /* PRE: dev->last_context != lock.context */
	        sis_context_switch(dev, dev->last_context, lock.context);
		/* POST: we will wait for the context
                   switch and will dispatch on a later call
                   when dev->last_context == lock.context
                   NOTE WE HOLD THE LOCK THROUGHOUT THIS
                   TIME! */
		current->policy |= SCHED_YIELD;
	        schedule();
	        current->state = TASK_RUNNING;
	        remove_wait_queue(&dev->context_wait, &entry);
	        if (signal_pending(current)) {
	                ret = -EINTR;
	        } else if (dev->last_context != lock.context) {
			DRM_ERROR("Context mismatch: %d %d\n",
                        	dev->last_context, lock.context);
	        }
	}
#endif

        if (!ret) {
                if (lock.flags & _DRM_LOCK_READY) {
				/* Wait for space in DMA/FIFO */
		}
                if (lock.flags & _DRM_LOCK_QUIESCENT) {
				/* Make hardware quiescent */
#if 0
                        sis_quiescent(dev);
#endif
		}
        }

        DRM_DEBUG("%d %s\n", lock.context, ret ? "interrupted" : "has lock");

#if DRM_DMA_HISTOGRAM
        atomic_inc(&dev->histo.lacq[drm_histogram_slot(get_cycles() - start)]);
#endif
        
        return ret;
}


int sis_unlock(struct inode *inode, struct file *filp, unsigned int cmd,
		 unsigned long arg)
{
	drm_file_t	  *priv	  = filp->private_data;
	drm_device_t	  *dev	  = priv->dev;
	drm_lock_t	  lock;

	copy_from_user_ret(&lock, (drm_lock_t *)arg, sizeof(lock), -EFAULT);
	
	if (lock.context == DRM_KERNEL_CONTEXT) {
		DRM_ERROR("Process %d using kernel context %d\n",
			  current->pid, lock.context);
		return -EINVAL;
	}

	DRM_DEBUG("%d frees lock (%d holds)\n",
		  lock.context,
		  _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
	atomic_inc(&dev->total_unlocks);
	if (_DRM_LOCK_IS_CONT(dev->lock.hw_lock->lock))
		atomic_inc(&dev->total_contends);
	drm_lock_transfer(dev, &dev->lock.hw_lock->lock, DRM_KERNEL_CONTEXT);
				/* FIXME: Try to send data to card here */
	if (!dev->context_flag) {
		if (drm_lock_free(dev, &dev->lock.hw_lock->lock,
				  DRM_KERNEL_CONTEXT)) {
			DRM_ERROR("\n");
		}
	}
	
	return 0;
}

module_init(sis_init);
module_exit(sis_cleanup);

#ifndef MODULE
/*
 * sis_setup is called by the kernel to parse command-line options passed
 * via the boot-loader (e.g., LILO).  It calls the insmod option routine,
 * drm_parse_options.
 */
static int __init sis_options(char *str)
{
	drm_parse_options(str);
	return 1;
}

__setup("sis=", sis_options);
#endif