1 /* i810_drv.c -- I810 driver -*- linux-c -*-
2 * Created: Mon Dec 13 01:56:22 1999 by jhartmann@precisioninsight.com
4 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
26 * Authors: Rickard E. (Rik) Faith <faith@precisioninsight.com>
27 * Jeff Hartmann <jhartmann@precisioninsight.com>
29 * $XFree86: xc/programs/Xserver/hw/xfree86/os-support/linux/drm/kernel/i810_drv.c,v 1.1 2000/02/11 17:26:05 dawes Exp $
38 EXPORT_SYMBOL(i810_init);
39 EXPORT_SYMBOL(i810_cleanup);
41 #define I810_NAME "i810"
42 #define I810_DESC "Intel I810"
43 #define I810_DATE "19991213"
46 #define I810_PATCHLEVEL 1
48 static drm_device_t i810_device;
49 drm_ctx_t i810_res_ctx;
51 static struct file_operations i810_fops = {
54 release: i810_release,
62 static struct miscdevice i810_misc = {
63 minor: MISC_DYNAMIC_MINOR,
68 static drm_ioctl_desc_t i810_ioctls[] = {
69 [DRM_IOCTL_NR(DRM_IOCTL_VERSION)] = { i810_version, 0, 0 },
70 [DRM_IOCTL_NR(DRM_IOCTL_GET_UNIQUE)] = { drm_getunique, 0, 0 },
71 [DRM_IOCTL_NR(DRM_IOCTL_GET_MAGIC)] = { drm_getmagic, 0, 0 },
72 [DRM_IOCTL_NR(DRM_IOCTL_IRQ_BUSID)] = { drm_irq_busid, 0, 1 },
74 [DRM_IOCTL_NR(DRM_IOCTL_SET_UNIQUE)] = { drm_setunique, 1, 1 },
75 [DRM_IOCTL_NR(DRM_IOCTL_BLOCK)] = { drm_block, 1, 1 },
76 [DRM_IOCTL_NR(DRM_IOCTL_UNBLOCK)] = { drm_unblock, 1, 1 },
77 [DRM_IOCTL_NR(DRM_IOCTL_CONTROL)] = { i810_control, 1, 1 },
78 [DRM_IOCTL_NR(DRM_IOCTL_AUTH_MAGIC)] = { drm_authmagic, 1, 1 },
79 [DRM_IOCTL_NR(DRM_IOCTL_ADD_MAP)] = { drm_addmap, 1, 1 },
80 [DRM_IOCTL_NR(DRM_IOCTL_ADD_BUFS)] = { i810_addbufs, 1, 1 },
81 [DRM_IOCTL_NR(DRM_IOCTL_MARK_BUFS)] = { i810_markbufs, 1, 1 },
82 [DRM_IOCTL_NR(DRM_IOCTL_INFO_BUFS)] = { i810_infobufs, 1, 0 },
83 [DRM_IOCTL_NR(DRM_IOCTL_MAP_BUFS)] = { i810_mapbufs, 1, 0 },
84 [DRM_IOCTL_NR(DRM_IOCTL_FREE_BUFS)] = { i810_freebufs, 1, 0 },
86 [DRM_IOCTL_NR(DRM_IOCTL_ADD_CTX)] = { i810_addctx, 1, 1 },
87 [DRM_IOCTL_NR(DRM_IOCTL_RM_CTX)] = { i810_rmctx, 1, 1 },
88 [DRM_IOCTL_NR(DRM_IOCTL_MOD_CTX)] = { i810_modctx, 1, 1 },
89 [DRM_IOCTL_NR(DRM_IOCTL_GET_CTX)] = { i810_getctx, 1, 0 },
90 [DRM_IOCTL_NR(DRM_IOCTL_SWITCH_CTX)] = { i810_switchctx, 1, 1 },
91 [DRM_IOCTL_NR(DRM_IOCTL_NEW_CTX)] = { i810_newctx, 1, 1 },
92 [DRM_IOCTL_NR(DRM_IOCTL_RES_CTX)] = { i810_resctx, 1, 0 },
93 [DRM_IOCTL_NR(DRM_IOCTL_ADD_DRAW)] = { drm_adddraw, 1, 1 },
94 [DRM_IOCTL_NR(DRM_IOCTL_RM_DRAW)] = { drm_rmdraw, 1, 1 },
96 [DRM_IOCTL_NR(DRM_IOCTL_DMA)] = { i810_dma, 1, 0 },
98 [DRM_IOCTL_NR(DRM_IOCTL_LOCK)] = { i810_lock, 1, 0 },
99 [DRM_IOCTL_NR(DRM_IOCTL_UNLOCK)] = { i810_unlock, 1, 0 },
100 [DRM_IOCTL_NR(DRM_IOCTL_FINISH)] = { drm_finish, 1, 0 },
102 [DRM_IOCTL_NR(DRM_IOCTL_AGP_ACQUIRE)] = { drm_agp_acquire, 1, 1 },
103 [DRM_IOCTL_NR(DRM_IOCTL_AGP_RELEASE)] = { drm_agp_release, 1, 1 },
104 [DRM_IOCTL_NR(DRM_IOCTL_AGP_ENABLE)] = { drm_agp_enable, 1, 1 },
105 [DRM_IOCTL_NR(DRM_IOCTL_AGP_INFO)] = { drm_agp_info, 1, 0 },
106 [DRM_IOCTL_NR(DRM_IOCTL_AGP_ALLOC)] = { drm_agp_alloc, 1, 1 },
107 [DRM_IOCTL_NR(DRM_IOCTL_AGP_FREE)] = { drm_agp_free, 1, 1 },
108 [DRM_IOCTL_NR(DRM_IOCTL_AGP_BIND)] = { drm_agp_bind, 1, 1 },
109 [DRM_IOCTL_NR(DRM_IOCTL_AGP_UNBIND)] = { drm_agp_unbind, 1, 1 },
110 [DRM_IOCTL_NR(DRM_IOCTL_I810_INIT)] = { i810_dma_init, 1, 1 },
111 [DRM_IOCTL_NR(DRM_IOCTL_I810_VERTEX)] = { i810_dma_vertex, 1, 0 },
112 [DRM_IOCTL_NR(DRM_IOCTL_I810_DMA)] = { i810_dma_general,1, 0 },
113 [DRM_IOCTL_NR(DRM_IOCTL_I810_FLUSH)] = { i810_flush_ioctl,1, 0 },
114 [DRM_IOCTL_NR(DRM_IOCTL_I810_GETAGE)] = { i810_getage, 1, 0 },
117 #define I810_IOCTL_COUNT DRM_ARRAY_SIZE(i810_ioctls)
120 int init_module(void);
121 void cleanup_module(void);
122 static char *i810 = NULL;
124 MODULE_AUTHOR("Precision Insight, Inc., Cedar Park, Texas.");
125 MODULE_DESCRIPTION("Intel I810");
126 MODULE_PARM(i810, "s");
128 /* init_module is called when insmod is used to load the module */
130 int init_module(void)
132 DRM_DEBUG("doing i810_init()\n");
136 /* cleanup_module is called when rmmod is used to unload the module */
138 void cleanup_module(void)
145 /* i810_setup is called by the kernel to parse command-line options passed
146 * via the boot-loader (e.g., LILO). It calls the insmod option routine,
149 * This is not currently supported, since it requires changes to
150 * linux/init/main.c. */
153 void __init i810_setup(char *str, int *ints)
156 DRM_ERROR("Illegal command line format, ignored\n");
159 drm_parse_options(str);
163 static int i810_setup(drm_device_t *dev)
167 atomic_set(&dev->ioctl_count, 0);
168 atomic_set(&dev->vma_count, 0);
170 atomic_set(&dev->buf_alloc, 0);
174 atomic_set(&dev->total_open, 0);
175 atomic_set(&dev->total_close, 0);
176 atomic_set(&dev->total_ioctl, 0);
177 atomic_set(&dev->total_irq, 0);
178 atomic_set(&dev->total_ctx, 0);
179 atomic_set(&dev->total_locks, 0);
180 atomic_set(&dev->total_unlocks, 0);
181 atomic_set(&dev->total_contends, 0);
182 atomic_set(&dev->total_sleeps, 0);
184 for (i = 0; i < DRM_HASH_SIZE; i++) {
185 dev->magiclist[i].head = NULL;
186 dev->magiclist[i].tail = NULL;
191 dev->lock.hw_lock = NULL;
192 init_waitqueue_head(&dev->lock.lock_queue);
193 dev->queue_count = 0;
194 dev->queue_reserved = 0;
195 dev->queue_slots = 0;
196 dev->queuelist = NULL;
198 dev->context_flag = 0;
199 dev->interrupt_flag = 0;
201 dev->last_context = 0;
202 dev->last_switch = 0;
203 dev->last_checked = 0;
204 init_timer(&dev->timer);
205 init_waitqueue_head(&dev->context_wait);
207 memset(&dev->histo, 0, sizeof(dev->histo));
212 dev->buf_rp = dev->buf;
213 dev->buf_wp = dev->buf;
214 dev->buf_end = dev->buf + DRM_BSZ;
215 dev->buf_async = NULL;
216 init_waitqueue_head(&dev->buf_readers);
217 init_waitqueue_head(&dev->buf_writers);
221 /* The kernel's context could be created here, but is now created
222 in drm_dma_enqueue. This is more resource-efficient for
223 hardware that does not do DMA, but may mean that
224 drm_select_queue fails between the time the interrupt is
225 initialized and the time the queues are initialized. */
231 static int i810_takedown(drm_device_t *dev)
234 drm_magic_entry_t *pt, *next;
236 drm_vma_entry_t *vma, *vma_next;
240 if (dev->irq) i810_irq_uninstall(dev);
242 down(&dev->struct_sem);
243 del_timer(&dev->timer);
246 drm_free(dev->devname, strlen(dev->devname)+1, DRM_MEM_DRIVER);
251 drm_free(dev->unique, strlen(dev->unique)+1, DRM_MEM_DRIVER);
256 for (i = 0; i < DRM_HASH_SIZE; i++) {
257 for (pt = dev->magiclist[i].head; pt; pt = next) {
259 drm_free(pt, sizeof(*pt), DRM_MEM_MAGIC);
261 dev->magiclist[i].head = dev->magiclist[i].tail = NULL;
263 /* Clear AGP information */
265 drm_agp_mem_t *entry;
266 drm_agp_mem_t *nexte;
268 /* Remove AGP resources, but leave dev->agp
269 intact until r128_cleanup is called. */
270 for (entry = dev->agp->memory; entry; entry = nexte) {
272 if (entry->bound) drm_unbind_agp(entry->memory);
273 drm_free_agp(entry->memory, entry->pages);
274 drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
276 dev->agp->memory = NULL;
278 if (dev->agp->acquired && drm_agp.release)
279 (*drm_agp.release)();
281 dev->agp->acquired = 0;
282 dev->agp->enabled = 0;
284 /* Clear vma list (only built for debugging) */
286 for (vma = dev->vmalist; vma; vma = vma_next) {
287 vma_next = vma->next;
288 drm_free(vma, sizeof(*vma), DRM_MEM_VMAS);
293 /* Clear map area and mtrr information */
295 for (i = 0; i < dev->map_count; i++) {
296 map = dev->maplist[i];
299 case _DRM_FRAME_BUFFER:
301 if (map->mtrr >= 0) {
303 retcode = mtrr_del(map->mtrr,
306 DRM_DEBUG("mtrr_del = %d\n", retcode);
309 drm_ioremapfree(map->handle, map->size);
312 drm_free_pages((unsigned long)map->handle,
320 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
322 drm_free(dev->maplist,
323 dev->map_count * sizeof(*dev->maplist),
329 if (dev->queuelist) {
330 for (i = 0; i < dev->queue_count; i++) {
331 drm_waitlist_destroy(&dev->queuelist[i]->waitlist);
332 if (dev->queuelist[i]) {
333 drm_free(dev->queuelist[i],
334 sizeof(*dev->queuelist[0]),
336 dev->queuelist[i] = NULL;
339 drm_free(dev->queuelist,
340 dev->queue_slots * sizeof(*dev->queuelist),
342 dev->queuelist = NULL;
345 drm_dma_takedown(dev);
347 dev->queue_count = 0;
348 if (dev->lock.hw_lock) {
349 dev->lock.hw_lock = NULL; /* SHM removed */
351 wake_up_interruptible(&dev->lock.lock_queue);
353 up(&dev->struct_sem);
358 /* i810_init is called via init_module at module load time, or via
359 * linux/init/main.c (this is not currently supported). */
364 drm_device_t *dev = &i810_device;
368 memset((void *)dev, 0, sizeof(*dev));
369 dev->count_lock = SPIN_LOCK_UNLOCKED;
370 sema_init(&dev->struct_sem, 1);
373 drm_parse_options(i810);
375 DRM_DEBUG("doing misc_register\n");
376 if ((retcode = misc_register(&i810_misc))) {
377 DRM_ERROR("Cannot register \"%s\"\n", I810_NAME);
380 dev->device = MKDEV(MISC_MAJOR, i810_misc.minor);
381 dev->name = I810_NAME;
383 DRM_DEBUG("doing mem init\n");
385 DRM_DEBUG("doing proc init\n");
387 DRM_DEBUG("doing agp init\n");
388 dev->agp = drm_agp_init();
389 if(dev->agp == NULL) {
390 DRM_INFO("The i810 drm module requires the agpgart module"
391 " to function correctly\nPlease load the agpgart"
392 " module before you load the i810 module\n");
394 misc_deregister(&i810_misc);
398 DRM_DEBUG("doing ctxbitmap init\n");
399 if((retcode = drm_ctxbitmap_init(dev))) {
400 DRM_ERROR("Cannot allocate memory for context bitmap.\n");
402 misc_deregister(&i810_misc);
407 DRM_INFO("Initialized %s %d.%d.%d %s on minor %d\n",
418 /* i810_cleanup is called via cleanup_module at module unload time. */
420 void i810_cleanup(void)
422 drm_device_t *dev = &i810_device;
427 if (misc_deregister(&i810_misc)) {
428 DRM_ERROR("Cannot unload module\n");
430 DRM_INFO("Module unloaded\n");
432 drm_ctxbitmap_cleanup(dev);
435 drm_free(dev->agp, sizeof(*dev->agp), DRM_MEM_AGPLISTS);
440 int i810_version(struct inode *inode, struct file *filp, unsigned int cmd,
443 drm_version_t version;
446 copy_from_user_ret(&version,
447 (drm_version_t *)arg,
451 #define DRM_COPY(name,value) \
452 len = strlen(value); \
453 if (len > name##_len) len = name##_len; \
454 name##_len = strlen(value); \
456 copy_to_user_ret(name, value, len, -EFAULT); \
459 version.version_major = I810_MAJOR;
460 version.version_minor = I810_MINOR;
461 version.version_patchlevel = I810_PATCHLEVEL;
463 DRM_COPY(version.name, I810_NAME);
464 DRM_COPY(version.date, I810_DATE);
465 DRM_COPY(version.desc, I810_DESC);
467 copy_to_user_ret((drm_version_t *)arg,
474 int i810_open(struct inode *inode, struct file *filp)
476 drm_device_t *dev = &i810_device;
479 DRM_DEBUG("open_count = %d\n", dev->open_count);
480 if (!(retcode = drm_open_helper(inode, filp, dev))) {
482 atomic_inc(&dev->total_open);
483 spin_lock(&dev->count_lock);
484 if (!dev->open_count++) {
485 spin_unlock(&dev->count_lock);
486 return i810_setup(dev);
488 spin_unlock(&dev->count_lock);
493 int i810_release(struct inode *inode, struct file *filp)
495 drm_file_t *priv = filp->private_data;
496 drm_device_t *dev = priv->dev;
499 DRM_DEBUG("pid = %d, device = 0x%x, open_count = %d\n",
500 current->pid, dev->device, dev->open_count);
502 if (_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock)
503 && dev->lock.pid == current->pid) {
504 i810_reclaim_buffers(dev, priv->pid);
505 DRM_ERROR("Process %d dead, freeing lock for context %d\n",
507 _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
509 &dev->lock.hw_lock->lock,
510 _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
512 /* FIXME: may require heavy-handed reset of
513 hardware at this point, possibly
514 processed via a callback to the X
517 /* The lock is required to reclaim buffers */
518 DECLARE_WAITQUEUE(entry, current);
519 add_wait_queue(&dev->lock.lock_queue, &entry);
521 if (!dev->lock.hw_lock) {
522 /* Device has been unregistered */
526 if (drm_lock_take(&dev->lock.hw_lock->lock,
527 DRM_KERNEL_CONTEXT)) {
528 dev->lock.pid = priv->pid;
529 dev->lock.lock_time = jiffies;
530 atomic_inc(&dev->total_locks);
531 break; /* Got lock */
534 atomic_inc(&dev->total_sleeps);
535 current->state = TASK_INTERRUPTIBLE;
537 if (signal_pending(current)) {
538 retcode = -ERESTARTSYS;
542 current->state = TASK_RUNNING;
543 remove_wait_queue(&dev->lock.lock_queue, &entry);
545 i810_reclaim_buffers(dev, priv->pid);
546 drm_lock_free(dev, &dev->lock.hw_lock->lock,
550 drm_fasync(-1, filp, 0);
552 down(&dev->struct_sem);
553 if (priv->prev) priv->prev->next = priv->next;
554 else dev->file_first = priv->next;
555 if (priv->next) priv->next->prev = priv->prev;
556 else dev->file_last = priv->prev;
557 up(&dev->struct_sem);
559 drm_free(priv, sizeof(*priv), DRM_MEM_FILES);
561 atomic_inc(&dev->total_close);
562 spin_lock(&dev->count_lock);
563 if (!--dev->open_count) {
564 if (atomic_read(&dev->ioctl_count) || dev->blocked) {
565 DRM_ERROR("Device busy: %d %d\n",
566 atomic_read(&dev->ioctl_count),
568 spin_unlock(&dev->count_lock);
571 spin_unlock(&dev->count_lock);
572 return i810_takedown(dev);
574 spin_unlock(&dev->count_lock);
578 /* drm_ioctl is called whenever a process performs an ioctl on /dev/drm. */
580 int i810_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
583 int nr = DRM_IOCTL_NR(cmd);
584 drm_file_t *priv = filp->private_data;
585 drm_device_t *dev = priv->dev;
587 drm_ioctl_desc_t *ioctl;
590 atomic_inc(&dev->ioctl_count);
591 atomic_inc(&dev->total_ioctl);
594 DRM_DEBUG("pid = %d, cmd = 0x%02x, nr = 0x%02x, dev 0x%x, auth = %d\n",
595 current->pid, cmd, nr, dev->device, priv->authenticated);
597 if (nr >= I810_IOCTL_COUNT) {
600 ioctl = &i810_ioctls[nr];
604 DRM_DEBUG("no function\n");
606 } else if ((ioctl->root_only && !capable(CAP_SYS_ADMIN))
607 || (ioctl->auth_needed && !priv->authenticated)) {
610 retcode = (func)(inode, filp, cmd, arg);
614 atomic_dec(&dev->ioctl_count);
618 int i810_unlock(struct inode *inode, struct file *filp, unsigned int cmd,
621 drm_file_t *priv = filp->private_data;
622 drm_device_t *dev = priv->dev;
625 copy_from_user_ret(&lock, (drm_lock_t *)arg, sizeof(lock), -EFAULT);
627 if (lock.context == DRM_KERNEL_CONTEXT) {
628 DRM_ERROR("Process %d using kernel context %d\n",
629 current->pid, lock.context);
633 DRM_DEBUG("%d frees lock (%d holds)\n",
635 _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
636 atomic_inc(&dev->total_unlocks);
637 if (_DRM_LOCK_IS_CONT(dev->lock.hw_lock->lock))
638 atomic_inc(&dev->total_contends);
639 drm_lock_transfer(dev, &dev->lock.hw_lock->lock, DRM_KERNEL_CONTEXT);
640 if (!dev->context_flag) {
641 if (drm_lock_free(dev, &dev->lock.hw_lock->lock,
642 DRM_KERNEL_CONTEXT)) {
646 #if DRM_DMA_HISTOGRAM
647 atomic_inc(&dev->histo.lhld[drm_histogram_slot(get_cycles()