1 /* mga_drv.c -- Matrox g200/g400 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.
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 * PRECISION INSIGHT 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 OTHER
25 * DEALINGS IN THE SOFTWARE.
27 * Authors: Rickard E. (Rik) Faith <faith@valinux.com>
28 * Jeff Hartmann <jhartmann@valinux.com>
33 #include <linux/config.h>
37 EXPORT_SYMBOL(mga_init);
38 EXPORT_SYMBOL(mga_cleanup);
40 #define MGA_NAME "mga"
41 #define MGA_DESC "Matrox g200/g400"
42 #define MGA_DATE "19991213"
45 #define MGA_PATCHLEVEL 0
47 static drm_device_t mga_device;
48 drm_ctx_t mga_res_ctx;
50 static struct file_operations mga_fops = {
61 static struct miscdevice mga_misc = {
62 minor: MISC_DYNAMIC_MINOR,
67 static drm_ioctl_desc_t mga_ioctls[] = {
68 [DRM_IOCTL_NR(DRM_IOCTL_VERSION)] = { mga_version, 0, 0 },
69 [DRM_IOCTL_NR(DRM_IOCTL_GET_UNIQUE)] = { drm_getunique, 0, 0 },
70 [DRM_IOCTL_NR(DRM_IOCTL_GET_MAGIC)] = { drm_getmagic, 0, 0 },
71 [DRM_IOCTL_NR(DRM_IOCTL_IRQ_BUSID)] = { drm_irq_busid, 0, 1 },
73 [DRM_IOCTL_NR(DRM_IOCTL_SET_UNIQUE)] = { drm_setunique, 1, 1 },
74 [DRM_IOCTL_NR(DRM_IOCTL_BLOCK)] = { drm_block, 1, 1 },
75 [DRM_IOCTL_NR(DRM_IOCTL_UNBLOCK)] = { drm_unblock, 1, 1 },
76 [DRM_IOCTL_NR(DRM_IOCTL_CONTROL)] = { mga_control, 1, 1 },
77 [DRM_IOCTL_NR(DRM_IOCTL_AUTH_MAGIC)] = { drm_authmagic, 1, 1 },
78 [DRM_IOCTL_NR(DRM_IOCTL_ADD_MAP)] = { drm_addmap, 1, 1 },
79 [DRM_IOCTL_NR(DRM_IOCTL_ADD_BUFS)] = { mga_addbufs, 1, 1 },
80 [DRM_IOCTL_NR(DRM_IOCTL_MARK_BUFS)] = { mga_markbufs, 1, 1 },
81 [DRM_IOCTL_NR(DRM_IOCTL_INFO_BUFS)] = { mga_infobufs, 1, 0 },
82 [DRM_IOCTL_NR(DRM_IOCTL_MAP_BUFS)] = { mga_mapbufs, 1, 0 },
83 [DRM_IOCTL_NR(DRM_IOCTL_FREE_BUFS)] = { mga_freebufs, 1, 0 },
85 [DRM_IOCTL_NR(DRM_IOCTL_ADD_CTX)] = { mga_addctx, 1, 1 },
86 [DRM_IOCTL_NR(DRM_IOCTL_RM_CTX)] = { mga_rmctx, 1, 1 },
87 [DRM_IOCTL_NR(DRM_IOCTL_MOD_CTX)] = { mga_modctx, 1, 1 },
88 [DRM_IOCTL_NR(DRM_IOCTL_GET_CTX)] = { mga_getctx, 1, 0 },
89 [DRM_IOCTL_NR(DRM_IOCTL_SWITCH_CTX)] = { mga_switchctx, 1, 1 },
90 [DRM_IOCTL_NR(DRM_IOCTL_NEW_CTX)] = { mga_newctx, 1, 1 },
91 [DRM_IOCTL_NR(DRM_IOCTL_RES_CTX)] = { mga_resctx, 1, 0 },
92 [DRM_IOCTL_NR(DRM_IOCTL_ADD_DRAW)] = { drm_adddraw, 1, 1 },
93 [DRM_IOCTL_NR(DRM_IOCTL_RM_DRAW)] = { drm_rmdraw, 1, 1 },
95 [DRM_IOCTL_NR(DRM_IOCTL_DMA)] = { mga_dma, 1, 0 },
97 [DRM_IOCTL_NR(DRM_IOCTL_LOCK)] = { mga_lock, 1, 0 },
98 [DRM_IOCTL_NR(DRM_IOCTL_UNLOCK)] = { mga_unlock, 1, 0 },
99 [DRM_IOCTL_NR(DRM_IOCTL_FINISH)] = { drm_finish, 1, 0 },
101 [DRM_IOCTL_NR(DRM_IOCTL_AGP_ACQUIRE)] = { drm_agp_acquire, 1, 1 },
102 [DRM_IOCTL_NR(DRM_IOCTL_AGP_RELEASE)] = { drm_agp_release, 1, 1 },
103 [DRM_IOCTL_NR(DRM_IOCTL_AGP_ENABLE)] = { drm_agp_enable, 1, 1 },
104 [DRM_IOCTL_NR(DRM_IOCTL_AGP_INFO)] = { drm_agp_info, 1, 0 },
105 [DRM_IOCTL_NR(DRM_IOCTL_AGP_ALLOC)] = { drm_agp_alloc, 1, 1 },
106 [DRM_IOCTL_NR(DRM_IOCTL_AGP_FREE)] = { drm_agp_free, 1, 1 },
107 [DRM_IOCTL_NR(DRM_IOCTL_AGP_BIND)] = { drm_agp_bind, 1, 1 },
108 [DRM_IOCTL_NR(DRM_IOCTL_AGP_UNBIND)] = { drm_agp_unbind, 1, 1 },
109 [DRM_IOCTL_NR(DRM_IOCTL_MGA_INIT)] = { mga_dma_init, 1, 1 },
110 [DRM_IOCTL_NR(DRM_IOCTL_MGA_SWAP)] = { mga_swap_bufs, 1, 0 },
111 [DRM_IOCTL_NR(DRM_IOCTL_MGA_CLEAR)] = { mga_clear_bufs, 1, 0 },
112 [DRM_IOCTL_NR(DRM_IOCTL_MGA_ILOAD)] = { mga_iload, 1, 0 },
113 [DRM_IOCTL_NR(DRM_IOCTL_MGA_VERTEX)] = { mga_vertex, 1, 0 },
114 [DRM_IOCTL_NR(DRM_IOCTL_MGA_FLUSH)] = { mga_flush_ioctl, 1, 0 },
115 [DRM_IOCTL_NR(DRM_IOCTL_MGA_INDICES)] = { mga_indices, 1, 0 },
118 #define MGA_IOCTL_COUNT DRM_ARRAY_SIZE(mga_ioctls)
121 int init_module(void);
122 void cleanup_module(void);
123 static char *mga = NULL;
125 MODULE_AUTHOR("Precision Insight, Inc., Cedar Park, Texas.");
126 MODULE_DESCRIPTION("Matrox g200/g400");
127 MODULE_PARM(mga, "s");
129 /* init_module is called when insmod is used to load the module */
131 int init_module(void)
133 DRM_DEBUG("doing mga_init()\n");
137 /* cleanup_module is called when rmmod is used to unload the module */
139 void cleanup_module(void)
146 /* mga_setup is called by the kernel to parse command-line options passed
147 * via the boot-loader (e.g., LILO). It calls the insmod option routine,
150 * This is not currently supported, since it requires changes to
151 * linux/init/main.c. */
154 void __init mga_setup(char *str, int *ints)
157 DRM_ERROR("Illegal command line format, ignored\n");
160 drm_parse_options(str);
164 static int mga_setup(drm_device_t *dev)
168 atomic_set(&dev->ioctl_count, 0);
169 atomic_set(&dev->vma_count, 0);
171 atomic_set(&dev->buf_alloc, 0);
175 atomic_set(&dev->total_open, 0);
176 atomic_set(&dev->total_close, 0);
177 atomic_set(&dev->total_ioctl, 0);
178 atomic_set(&dev->total_irq, 0);
179 atomic_set(&dev->total_ctx, 0);
180 atomic_set(&dev->total_locks, 0);
181 atomic_set(&dev->total_unlocks, 0);
182 atomic_set(&dev->total_contends, 0);
183 atomic_set(&dev->total_sleeps, 0);
185 for (i = 0; i < DRM_HASH_SIZE; i++) {
186 dev->magiclist[i].head = NULL;
187 dev->magiclist[i].tail = NULL;
192 dev->lock.hw_lock = NULL;
193 init_waitqueue_head(&dev->lock.lock_queue);
194 dev->queue_count = 0;
195 dev->queue_reserved = 0;
196 dev->queue_slots = 0;
197 dev->queuelist = NULL;
199 dev->context_flag = 0;
200 dev->interrupt_flag = 0;
202 dev->last_context = 0;
203 dev->last_switch = 0;
204 dev->last_checked = 0;
205 init_timer(&dev->timer);
206 init_waitqueue_head(&dev->context_wait);
211 dev->buf_rp = dev->buf;
212 dev->buf_wp = dev->buf;
213 dev->buf_end = dev->buf + DRM_BSZ;
214 dev->buf_async = NULL;
215 init_waitqueue_head(&dev->buf_readers);
216 init_waitqueue_head(&dev->buf_writers);
220 /* The kernel's context could be created here, but is now created
221 in drm_dma_enqueue. This is more resource-efficient for
222 hardware that does not do DMA, but may mean that
223 drm_select_queue fails between the time the interrupt is
224 initialized and the time the queues are initialized. */
230 static int mga_takedown(drm_device_t *dev)
233 drm_magic_entry_t *pt, *next;
235 drm_vma_entry_t *vma, *vma_next;
239 if (dev->irq) mga_irq_uninstall(dev);
241 down(&dev->struct_sem);
242 del_timer(&dev->timer);
245 drm_free(dev->devname, strlen(dev->devname)+1, DRM_MEM_DRIVER);
250 drm_free(dev->unique, strlen(dev->unique)+1, DRM_MEM_DRIVER);
255 for (i = 0; i < DRM_HASH_SIZE; i++) {
256 for (pt = dev->magiclist[i].head; pt; pt = next) {
258 drm_free(pt, sizeof(*pt), DRM_MEM_MAGIC);
260 dev->magiclist[i].head = dev->magiclist[i].tail = NULL;
262 /* Clear AGP information */
264 drm_agp_mem_t *entry;
265 drm_agp_mem_t *nexte;
267 /* Remove AGP resources, but leave dev->agp
268 intact until cleanup is called. */
269 for (entry = dev->agp->memory; entry; entry = nexte) {
271 if (entry->bound) drm_unbind_agp(entry->memory);
272 drm_free_agp(entry->memory, entry->pages);
273 drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
275 dev->agp->memory = NULL;
277 if (dev->agp->acquired && drm_agp.release)
278 (*drm_agp.release)();
280 dev->agp->acquired = 0;
281 dev->agp->enabled = 0;
283 /* Clear vma list (only built for debugging) */
285 for (vma = dev->vmalist; vma; vma = vma_next) {
286 vma_next = vma->next;
287 drm_free(vma, sizeof(*vma), DRM_MEM_VMAS);
292 /* Clear map area and mtrr information */
294 for (i = 0; i < dev->map_count; i++) {
295 map = dev->maplist[i];
298 case _DRM_FRAME_BUFFER:
300 if (map->mtrr >= 0) {
302 retcode = mtrr_del(map->mtrr,
305 DRM_DEBUG("mtrr_del = %d\n", retcode);
308 drm_ioremapfree(map->handle, map->size);
311 drm_free_pages((unsigned long)map->handle,
319 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
321 drm_free(dev->maplist,
322 dev->map_count * sizeof(*dev->maplist),
328 if (dev->queuelist) {
329 for (i = 0; i < dev->queue_count; i++) {
330 drm_waitlist_destroy(&dev->queuelist[i]->waitlist);
331 if (dev->queuelist[i]) {
332 drm_free(dev->queuelist[i],
333 sizeof(*dev->queuelist[0]),
335 dev->queuelist[i] = NULL;
338 drm_free(dev->queuelist,
339 dev->queue_slots * sizeof(*dev->queuelist),
341 dev->queuelist = NULL;
344 drm_dma_takedown(dev);
346 dev->queue_count = 0;
347 if (dev->lock.hw_lock) {
348 dev->lock.hw_lock = NULL; /* SHM removed */
350 wake_up_interruptible(&dev->lock.lock_queue);
352 up(&dev->struct_sem);
357 /* mga_init is called via init_module at module load time, or via
358 * linux/init/main.c (this is not currently supported). */
363 drm_device_t *dev = &mga_device;
367 memset((void *)dev, 0, sizeof(*dev));
368 dev->count_lock = SPIN_LOCK_UNLOCKED;
369 sema_init(&dev->struct_sem, 1);
372 drm_parse_options(mga);
374 DRM_DEBUG("doing misc_register\n");
375 if ((retcode = misc_register(&mga_misc))) {
376 DRM_ERROR("Cannot register \"%s\"\n", MGA_NAME);
379 dev->device = MKDEV(MISC_MAJOR, mga_misc.minor);
380 dev->name = MGA_NAME;
382 DRM_DEBUG("doing mem init\n");
384 DRM_DEBUG("doing proc init\n");
386 DRM_DEBUG("doing agp init\n");
387 dev->agp = drm_agp_init();
388 if(dev->agp == NULL) {
389 DRM_INFO("The mga drm module requires the agpgart module"
390 " to function correctly\nPlease load the agpgart"
391 " module before you load the mga module\n");
393 misc_deregister(&mga_misc);
398 dev->agp->agp_mtrr = mtrr_add(dev->agp->agp_info.aper_base,
399 dev->agp->agp_info.aper_size * 1024 * 1024,
403 DRM_DEBUG("doing ctxbitmap init\n");
404 if((retcode = drm_ctxbitmap_init(dev))) {
405 DRM_ERROR("Cannot allocate memory for context bitmap.\n");
407 misc_deregister(&mga_misc);
412 DRM_INFO("Initialized %s %d.%d.%d %s on minor %d\n",
423 /* mga_cleanup is called via cleanup_module at module unload time. */
425 void mga_cleanup(void)
427 drm_device_t *dev = &mga_device;
432 if (misc_deregister(&mga_misc)) {
433 DRM_ERROR("Cannot unload module\n");
435 DRM_INFO("Module unloaded\n");
437 drm_ctxbitmap_cleanup(dev);
438 mga_dma_cleanup(dev);
440 if(dev->agp && dev->agp->agp_mtrr) {
442 retval = mtrr_del(dev->agp->agp_mtrr,
443 dev->agp->agp_info.aper_base,
444 dev->agp->agp_info.aper_size * 1024*1024);
445 DRM_DEBUG("mtrr_del = %d\n", retval);
451 drm_free(dev->agp, sizeof(*dev->agp), DRM_MEM_AGPLISTS);
456 int mga_version(struct inode *inode, struct file *filp, unsigned int cmd,
459 drm_version_t version;
462 copy_from_user_ret(&version,
463 (drm_version_t *)arg,
467 #define DRM_COPY(name,value) \
468 len = strlen(value); \
469 if (len > name##_len) len = name##_len; \
470 name##_len = strlen(value); \
472 copy_to_user_ret(name, value, len, -EFAULT); \
475 version.version_major = MGA_MAJOR;
476 version.version_minor = MGA_MINOR;
477 version.version_patchlevel = MGA_PATCHLEVEL;
479 DRM_COPY(version.name, MGA_NAME);
480 DRM_COPY(version.date, MGA_DATE);
481 DRM_COPY(version.desc, MGA_DESC);
483 copy_to_user_ret((drm_version_t *)arg,
490 int mga_open(struct inode *inode, struct file *filp)
492 drm_device_t *dev = &mga_device;
495 DRM_DEBUG("open_count = %d\n", dev->open_count);
496 if (!(retcode = drm_open_helper(inode, filp, dev))) {
498 atomic_inc(&dev->total_open);
499 spin_lock(&dev->count_lock);
500 if (!dev->open_count++) {
501 spin_unlock(&dev->count_lock);
502 return mga_setup(dev);
504 spin_unlock(&dev->count_lock);
509 int mga_release(struct inode *inode, struct file *filp)
511 drm_file_t *priv = filp->private_data;
512 drm_device_t *dev = priv->dev;
515 DRM_DEBUG("pid = %d, device = 0x%x, open_count = %d\n",
516 current->pid, dev->device, dev->open_count);
518 if (dev->lock.hw_lock && _DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock)
519 && dev->lock.pid == current->pid) {
520 mga_reclaim_buffers(dev, priv->pid);
521 DRM_ERROR("Process %d dead, freeing lock for context %d\n",
523 _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
525 &dev->lock.hw_lock->lock,
526 _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
528 /* FIXME: may require heavy-handed reset of
529 hardware at this point, possibly
530 processed via a callback to the X
532 } else if (dev->lock.hw_lock) {
533 /* The lock is required to reclaim buffers */
534 DECLARE_WAITQUEUE(entry, current);
535 add_wait_queue(&dev->lock.lock_queue, &entry);
537 if (!dev->lock.hw_lock) {
538 /* Device has been unregistered */
542 if (drm_lock_take(&dev->lock.hw_lock->lock,
543 DRM_KERNEL_CONTEXT)) {
544 dev->lock.pid = priv->pid;
545 dev->lock.lock_time = jiffies;
546 atomic_inc(&dev->total_locks);
547 break; /* Got lock */
550 atomic_inc(&dev->total_sleeps);
551 current->state = TASK_INTERRUPTIBLE;
553 if (signal_pending(current)) {
554 retcode = -ERESTARTSYS;
558 current->state = TASK_RUNNING;
559 remove_wait_queue(&dev->lock.lock_queue, &entry);
561 mga_reclaim_buffers(dev, priv->pid);
562 drm_lock_free(dev, &dev->lock.hw_lock->lock,
566 drm_fasync(-1, filp, 0);
568 down(&dev->struct_sem);
569 if (priv->prev) priv->prev->next = priv->next;
570 else dev->file_first = priv->next;
571 if (priv->next) priv->next->prev = priv->prev;
572 else dev->file_last = priv->prev;
573 up(&dev->struct_sem);
575 drm_free(priv, sizeof(*priv), DRM_MEM_FILES);
577 atomic_inc(&dev->total_close);
578 spin_lock(&dev->count_lock);
579 if (!--dev->open_count) {
580 if (atomic_read(&dev->ioctl_count) || dev->blocked) {
581 DRM_ERROR("Device busy: %d %d\n",
582 atomic_read(&dev->ioctl_count),
584 spin_unlock(&dev->count_lock);
587 spin_unlock(&dev->count_lock);
588 return mga_takedown(dev);
590 spin_unlock(&dev->count_lock);
595 /* drm_ioctl is called whenever a process performs an ioctl on /dev/drm. */
597 int mga_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
600 int nr = DRM_IOCTL_NR(cmd);
601 drm_file_t *priv = filp->private_data;
602 drm_device_t *dev = priv->dev;
604 drm_ioctl_desc_t *ioctl;
607 atomic_inc(&dev->ioctl_count);
608 atomic_inc(&dev->total_ioctl);
611 DRM_DEBUG("pid = %d, cmd = 0x%02x, nr = 0x%02x, dev 0x%x, auth = %d\n",
612 current->pid, cmd, nr, dev->device, priv->authenticated);
614 if (nr >= MGA_IOCTL_COUNT) {
617 ioctl = &mga_ioctls[nr];
621 DRM_DEBUG("no function\n");
623 } else if ((ioctl->root_only && !capable(CAP_SYS_ADMIN))
624 || (ioctl->auth_needed && !priv->authenticated)) {
627 retcode = (func)(inode, filp, cmd, arg);
631 atomic_dec(&dev->ioctl_count);
635 int mga_unlock(struct inode *inode, struct file *filp, unsigned int cmd,
638 drm_file_t *priv = filp->private_data;
639 drm_device_t *dev = priv->dev;
642 copy_from_user_ret(&lock, (drm_lock_t *)arg, sizeof(lock), -EFAULT);
644 if (lock.context == DRM_KERNEL_CONTEXT) {
645 DRM_ERROR("Process %d using kernel context %d\n",
646 current->pid, lock.context);
650 DRM_DEBUG("%d frees lock (%d holds)\n",
652 _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
653 atomic_inc(&dev->total_unlocks);
654 if (_DRM_LOCK_IS_CONT(dev->lock.hw_lock->lock))
655 atomic_inc(&dev->total_contends);
656 drm_lock_transfer(dev, &dev->lock.hw_lock->lock, DRM_KERNEL_CONTEXT);
657 mga_dma_schedule(dev, 1);
659 if (drm_lock_free(dev, &dev->lock.hw_lock->lock,
660 DRM_KERNEL_CONTEXT)) {