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.
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/mga_drv.c,v 1.1 2000/02/11 17:26:07 dawes Exp $
36 EXPORT_SYMBOL(mga_init);
37 EXPORT_SYMBOL(mga_cleanup);
39 #define MGA_NAME "mga"
40 #define MGA_DESC "Matrox g200/g400"
41 #define MGA_DATE "19991213"
44 #define MGA_PATCHLEVEL 1
46 static drm_device_t mga_device;
47 drm_ctx_t mga_res_ctx;
49 static struct file_operations mga_fops = {
60 static struct miscdevice mga_misc = {
61 minor: MISC_DYNAMIC_MINOR,
66 static drm_ioctl_desc_t mga_ioctls[] = {
67 [DRM_IOCTL_NR(DRM_IOCTL_VERSION)] = { mga_version, 0, 0 },
68 [DRM_IOCTL_NR(DRM_IOCTL_GET_UNIQUE)] = { drm_getunique, 0, 0 },
69 [DRM_IOCTL_NR(DRM_IOCTL_GET_MAGIC)] = { drm_getmagic, 0, 0 },
70 [DRM_IOCTL_NR(DRM_IOCTL_IRQ_BUSID)] = { drm_irq_busid, 0, 1 },
72 [DRM_IOCTL_NR(DRM_IOCTL_SET_UNIQUE)] = { drm_setunique, 1, 1 },
73 [DRM_IOCTL_NR(DRM_IOCTL_BLOCK)] = { drm_block, 1, 1 },
74 [DRM_IOCTL_NR(DRM_IOCTL_UNBLOCK)] = { drm_unblock, 1, 1 },
75 [DRM_IOCTL_NR(DRM_IOCTL_CONTROL)] = { mga_control, 1, 1 },
76 [DRM_IOCTL_NR(DRM_IOCTL_AUTH_MAGIC)] = { drm_authmagic, 1, 1 },
77 [DRM_IOCTL_NR(DRM_IOCTL_ADD_MAP)] = { drm_addmap, 1, 1 },
78 [DRM_IOCTL_NR(DRM_IOCTL_ADD_BUFS)] = { mga_addbufs, 1, 1 },
79 [DRM_IOCTL_NR(DRM_IOCTL_MARK_BUFS)] = { mga_markbufs, 1, 1 },
80 [DRM_IOCTL_NR(DRM_IOCTL_INFO_BUFS)] = { mga_infobufs, 1, 0 },
81 [DRM_IOCTL_NR(DRM_IOCTL_MAP_BUFS)] = { mga_mapbufs, 1, 0 },
82 [DRM_IOCTL_NR(DRM_IOCTL_FREE_BUFS)] = { mga_freebufs, 1, 0 },
84 [DRM_IOCTL_NR(DRM_IOCTL_ADD_CTX)] = { mga_addctx, 1, 1 },
85 [DRM_IOCTL_NR(DRM_IOCTL_RM_CTX)] = { mga_rmctx, 1, 1 },
86 [DRM_IOCTL_NR(DRM_IOCTL_MOD_CTX)] = { mga_modctx, 1, 1 },
87 [DRM_IOCTL_NR(DRM_IOCTL_GET_CTX)] = { mga_getctx, 1, 0 },
88 [DRM_IOCTL_NR(DRM_IOCTL_SWITCH_CTX)] = { mga_switchctx, 1, 1 },
89 [DRM_IOCTL_NR(DRM_IOCTL_NEW_CTX)] = { mga_newctx, 1, 1 },
90 [DRM_IOCTL_NR(DRM_IOCTL_RES_CTX)] = { mga_resctx, 1, 0 },
91 [DRM_IOCTL_NR(DRM_IOCTL_ADD_DRAW)] = { drm_adddraw, 1, 1 },
92 [DRM_IOCTL_NR(DRM_IOCTL_RM_DRAW)] = { drm_rmdraw, 1, 1 },
94 [DRM_IOCTL_NR(DRM_IOCTL_DMA)] = { mga_dma, 1, 0 },
96 [DRM_IOCTL_NR(DRM_IOCTL_LOCK)] = { mga_lock, 1, 0 },
97 [DRM_IOCTL_NR(DRM_IOCTL_UNLOCK)] = { mga_unlock, 1, 0 },
98 [DRM_IOCTL_NR(DRM_IOCTL_FINISH)] = { drm_finish, 1, 0 },
100 [DRM_IOCTL_NR(DRM_IOCTL_AGP_ACQUIRE)] = { drm_agp_acquire, 1, 1 },
101 [DRM_IOCTL_NR(DRM_IOCTL_AGP_RELEASE)] = { drm_agp_release, 1, 1 },
102 [DRM_IOCTL_NR(DRM_IOCTL_AGP_ENABLE)] = { drm_agp_enable, 1, 1 },
103 [DRM_IOCTL_NR(DRM_IOCTL_AGP_INFO)] = { drm_agp_info, 1, 0 },
104 [DRM_IOCTL_NR(DRM_IOCTL_AGP_ALLOC)] = { drm_agp_alloc, 1, 1 },
105 [DRM_IOCTL_NR(DRM_IOCTL_AGP_FREE)] = { drm_agp_free, 1, 1 },
106 [DRM_IOCTL_NR(DRM_IOCTL_AGP_BIND)] = { drm_agp_bind, 1, 1 },
107 [DRM_IOCTL_NR(DRM_IOCTL_AGP_UNBIND)] = { drm_agp_unbind, 1, 1 },
108 [DRM_IOCTL_NR(DRM_IOCTL_MGA_INIT)] = { mga_dma_init, 1, 1 },
109 [DRM_IOCTL_NR(DRM_IOCTL_MGA_SWAP)] = { mga_swap_bufs, 1, 0 },
110 [DRM_IOCTL_NR(DRM_IOCTL_MGA_CLEAR)] = { mga_clear_bufs, 1, 0 },
111 [DRM_IOCTL_NR(DRM_IOCTL_MGA_ILOAD)] = { mga_iload, 1, 0 },
112 [DRM_IOCTL_NR(DRM_IOCTL_MGA_VERTEX)] = { mga_vertex, 1, 0 },
113 [DRM_IOCTL_NR(DRM_IOCTL_MGA_FLUSH)] = { mga_flush_ioctl, 1, 0 },
116 #define MGA_IOCTL_COUNT DRM_ARRAY_SIZE(mga_ioctls)
119 int init_module(void);
120 void cleanup_module(void);
121 static char *mga = NULL;
123 MODULE_AUTHOR("Precision Insight, Inc., Cedar Park, Texas.");
124 MODULE_DESCRIPTION("Matrox g200/g400");
125 MODULE_PARM(mga, "s");
127 /* init_module is called when insmod is used to load the module */
129 int init_module(void)
131 DRM_DEBUG("doing mga_init()\n");
135 /* cleanup_module is called when rmmod is used to unload the module */
137 void cleanup_module(void)
144 /* mga_setup is called by the kernel to parse command-line options passed
145 * via the boot-loader (e.g., LILO). It calls the insmod option routine,
148 * This is not currently supported, since it requires changes to
149 * linux/init/main.c. */
152 void __init mga_setup(char *str, int *ints)
155 DRM_ERROR("Illegal command line format, ignored\n");
158 drm_parse_options(str);
162 static int mga_setup(drm_device_t *dev)
166 atomic_set(&dev->ioctl_count, 0);
167 atomic_set(&dev->vma_count, 0);
169 atomic_set(&dev->buf_alloc, 0);
173 atomic_set(&dev->total_open, 0);
174 atomic_set(&dev->total_close, 0);
175 atomic_set(&dev->total_ioctl, 0);
176 atomic_set(&dev->total_irq, 0);
177 atomic_set(&dev->total_ctx, 0);
178 atomic_set(&dev->total_locks, 0);
179 atomic_set(&dev->total_unlocks, 0);
180 atomic_set(&dev->total_contends, 0);
181 atomic_set(&dev->total_sleeps, 0);
183 for (i = 0; i < DRM_HASH_SIZE; i++) {
184 dev->magiclist[i].head = NULL;
185 dev->magiclist[i].tail = NULL;
190 dev->lock.hw_lock = NULL;
191 init_waitqueue_head(&dev->lock.lock_queue);
192 dev->queue_count = 0;
193 dev->queue_reserved = 0;
194 dev->queue_slots = 0;
195 dev->queuelist = NULL;
197 dev->context_flag = 0;
198 dev->interrupt_flag = 0;
200 dev->last_context = 0;
201 dev->last_switch = 0;
202 dev->last_checked = 0;
203 init_timer(&dev->timer);
204 init_waitqueue_head(&dev->context_wait);
209 dev->buf_rp = dev->buf;
210 dev->buf_wp = dev->buf;
211 dev->buf_end = dev->buf + DRM_BSZ;
212 dev->buf_async = NULL;
213 init_waitqueue_head(&dev->buf_readers);
214 init_waitqueue_head(&dev->buf_writers);
218 /* The kernel's context could be created here, but is now created
219 in drm_dma_enqueue. This is more resource-efficient for
220 hardware that does not do DMA, but may mean that
221 drm_select_queue fails between the time the interrupt is
222 initialized and the time the queues are initialized. */
228 static int mga_takedown(drm_device_t *dev)
231 drm_magic_entry_t *pt, *next;
233 drm_vma_entry_t *vma, *vma_next;
237 if (dev->irq) mga_irq_uninstall(dev);
239 down(&dev->struct_sem);
240 del_timer(&dev->timer);
243 drm_free(dev->devname, strlen(dev->devname)+1, DRM_MEM_DRIVER);
248 drm_free(dev->unique, strlen(dev->unique)+1, DRM_MEM_DRIVER);
253 for (i = 0; i < DRM_HASH_SIZE; i++) {
254 for (pt = dev->magiclist[i].head; pt; pt = next) {
256 drm_free(pt, sizeof(*pt), DRM_MEM_MAGIC);
258 dev->magiclist[i].head = dev->magiclist[i].tail = NULL;
260 /* Clear AGP information */
262 drm_agp_mem_t *entry;
263 drm_agp_mem_t *nexte;
265 /* Remove AGP resources, but leave dev->agp
266 intact until cleanup is called. */
267 for (entry = dev->agp->memory; entry; entry = nexte) {
269 if (entry->bound) drm_unbind_agp(entry->memory);
270 drm_free_agp(entry->memory, entry->pages);
271 drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
273 dev->agp->memory = NULL;
275 if (dev->agp->acquired && drm_agp.release)
276 (*drm_agp.release)();
278 dev->agp->acquired = 0;
279 dev->agp->enabled = 0;
281 /* Clear vma list (only built for debugging) */
283 for (vma = dev->vmalist; vma; vma = vma_next) {
284 vma_next = vma->next;
285 drm_free(vma, sizeof(*vma), DRM_MEM_VMAS);
290 /* Clear map area and mtrr information */
292 for (i = 0; i < dev->map_count; i++) {
293 map = dev->maplist[i];
296 case _DRM_FRAME_BUFFER:
298 if (map->mtrr >= 0) {
300 retcode = mtrr_del(map->mtrr,
303 DRM_DEBUG("mtrr_del = %d\n", retcode);
306 drm_ioremapfree(map->handle, map->size);
309 drm_free_pages((unsigned long)map->handle,
317 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
319 drm_free(dev->maplist,
320 dev->map_count * sizeof(*dev->maplist),
326 if (dev->queuelist) {
327 for (i = 0; i < dev->queue_count; i++) {
328 drm_waitlist_destroy(&dev->queuelist[i]->waitlist);
329 if (dev->queuelist[i]) {
330 drm_free(dev->queuelist[i],
331 sizeof(*dev->queuelist[0]),
333 dev->queuelist[i] = NULL;
336 drm_free(dev->queuelist,
337 dev->queue_slots * sizeof(*dev->queuelist),
339 dev->queuelist = NULL;
342 drm_dma_takedown(dev);
344 dev->queue_count = 0;
345 if (dev->lock.hw_lock) {
346 dev->lock.hw_lock = NULL; /* SHM removed */
348 wake_up_interruptible(&dev->lock.lock_queue);
350 up(&dev->struct_sem);
355 /* mga_init is called via init_module at module load time, or via
356 * linux/init/main.c (this is not currently supported). */
361 drm_device_t *dev = &mga_device;
365 memset((void *)dev, 0, sizeof(*dev));
366 dev->count_lock = SPIN_LOCK_UNLOCKED;
367 sema_init(&dev->struct_sem, 1);
370 drm_parse_options(mga);
372 DRM_DEBUG("doing misc_register\n");
373 if ((retcode = misc_register(&mga_misc))) {
374 DRM_ERROR("Cannot register \"%s\"\n", MGA_NAME);
377 dev->device = MKDEV(MISC_MAJOR, mga_misc.minor);
378 dev->name = MGA_NAME;
380 DRM_DEBUG("doing mem init\n");
382 DRM_DEBUG("doing proc init\n");
384 DRM_DEBUG("doing agp init\n");
385 dev->agp = drm_agp_init();
386 if(dev->agp == NULL) {
387 DRM_DEBUG("The mga drm module requires the agpgart module"
388 " to function correctly\nPlease load the agpgart"
389 " module before you load the mga module\n");
391 misc_deregister(&mga_misc);
396 dev->agp->agp_mtrr = mtrr_add(dev->agp->agp_info.aper_base,
397 dev->agp->agp_info.aper_size * 1024 * 1024,
401 DRM_DEBUG("doing ctxbitmap init\n");
402 if((retcode = drm_ctxbitmap_init(dev))) {
403 DRM_ERROR("Cannot allocate memory for context bitmap.\n");
405 misc_deregister(&mga_misc);
410 DRM_INFO("Initialized %s %d.%d.%d %s on minor %d\n",
421 /* mga_cleanup is called via cleanup_module at module unload time. */
423 void mga_cleanup(void)
425 drm_device_t *dev = &mga_device;
430 if (misc_deregister(&mga_misc)) {
431 DRM_ERROR("Cannot unload module\n");
433 DRM_INFO("Module unloaded\n");
435 drm_ctxbitmap_cleanup(dev);
436 mga_dma_cleanup(dev);
438 if(dev->agp && dev->agp->agp_mtrr) {
440 retval = mtrr_del(dev->agp->agp_mtrr,
441 dev->agp->agp_info.aper_base,
442 dev->agp->agp_info.aper_size * 1024*1024);
443 DRM_DEBUG("mtrr_del = %d\n", retval);
449 drm_free(dev->agp, sizeof(*dev->agp), DRM_MEM_AGPLISTS);
454 int mga_version(struct inode *inode, struct file *filp, unsigned int cmd,
457 drm_version_t version;
460 copy_from_user_ret(&version,
461 (drm_version_t *)arg,
465 #define DRM_COPY(name,value) \
466 len = strlen(value); \
467 if (len > name##_len) len = name##_len; \
468 name##_len = strlen(value); \
470 copy_to_user_ret(name, value, len, -EFAULT); \
473 version.version_major = MGA_MAJOR;
474 version.version_minor = MGA_MINOR;
475 version.version_patchlevel = MGA_PATCHLEVEL;
477 DRM_COPY(version.name, MGA_NAME);
478 DRM_COPY(version.date, MGA_DATE);
479 DRM_COPY(version.desc, MGA_DESC);
481 copy_to_user_ret((drm_version_t *)arg,
488 int mga_open(struct inode *inode, struct file *filp)
490 drm_device_t *dev = &mga_device;
493 DRM_DEBUG("open_count = %d\n", dev->open_count);
494 if (!(retcode = drm_open_helper(inode, filp, dev))) {
496 atomic_inc(&dev->total_open);
497 spin_lock(&dev->count_lock);
498 if (!dev->open_count++) {
499 spin_unlock(&dev->count_lock);
500 return mga_setup(dev);
502 spin_unlock(&dev->count_lock);
507 int mga_release(struct inode *inode, struct file *filp)
509 drm_file_t *priv = filp->private_data;
510 drm_device_t *dev = priv->dev;
513 DRM_DEBUG("pid = %d, device = 0x%x, open_count = %d\n",
514 current->pid, dev->device, dev->open_count);
516 if (dev->lock.hw_lock && _DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock)
517 && dev->lock.pid == current->pid) {
518 mga_reclaim_buffers(dev, priv->pid);
519 DRM_ERROR("Process %d dead, freeing lock for context %d\n",
521 _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
523 &dev->lock.hw_lock->lock,
524 _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
526 /* FIXME: may require heavy-handed reset of
527 hardware at this point, possibly
528 processed via a callback to the X
530 } else if (dev->lock.hw_lock) {
531 /* The lock is required to reclaim buffers */
532 DECLARE_WAITQUEUE(entry, current);
533 add_wait_queue(&dev->lock.lock_queue, &entry);
535 if (!dev->lock.hw_lock) {
536 /* Device has been unregistered */
540 if (drm_lock_take(&dev->lock.hw_lock->lock,
541 DRM_KERNEL_CONTEXT)) {
542 dev->lock.pid = priv->pid;
543 dev->lock.lock_time = jiffies;
544 atomic_inc(&dev->total_locks);
545 break; /* Got lock */
548 atomic_inc(&dev->total_sleeps);
549 current->state = TASK_INTERRUPTIBLE;
551 if (signal_pending(current)) {
552 retcode = -ERESTARTSYS;
556 current->state = TASK_RUNNING;
557 remove_wait_queue(&dev->lock.lock_queue, &entry);
559 mga_reclaim_buffers(dev, priv->pid);
560 drm_lock_free(dev, &dev->lock.hw_lock->lock,
564 drm_fasync(-1, filp, 0);
566 down(&dev->struct_sem);
567 if (priv->prev) priv->prev->next = priv->next;
568 else dev->file_first = priv->next;
569 if (priv->next) priv->next->prev = priv->prev;
570 else dev->file_last = priv->prev;
571 up(&dev->struct_sem);
573 drm_free(priv, sizeof(*priv), DRM_MEM_FILES);
575 atomic_inc(&dev->total_close);
576 spin_lock(&dev->count_lock);
577 if (!--dev->open_count) {
578 if (atomic_read(&dev->ioctl_count) || dev->blocked) {
579 DRM_ERROR("Device busy: %d %d\n",
580 atomic_read(&dev->ioctl_count),
582 spin_unlock(&dev->count_lock);
585 spin_unlock(&dev->count_lock);
586 return mga_takedown(dev);
588 spin_unlock(&dev->count_lock);
593 /* drm_ioctl is called whenever a process performs an ioctl on /dev/drm. */
595 int mga_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
598 int nr = DRM_IOCTL_NR(cmd);
599 drm_file_t *priv = filp->private_data;
600 drm_device_t *dev = priv->dev;
602 drm_ioctl_desc_t *ioctl;
605 atomic_inc(&dev->ioctl_count);
606 atomic_inc(&dev->total_ioctl);
609 DRM_DEBUG("pid = %d, cmd = 0x%02x, nr = 0x%02x, dev 0x%x, auth = %d\n",
610 current->pid, cmd, nr, dev->device, priv->authenticated);
612 if (nr >= MGA_IOCTL_COUNT) {
615 ioctl = &mga_ioctls[nr];
619 DRM_DEBUG("no function\n");
621 } else if ((ioctl->root_only && !capable(CAP_SYS_ADMIN))
622 || (ioctl->auth_needed && !priv->authenticated)) {
625 retcode = (func)(inode, filp, cmd, arg);
629 atomic_dec(&dev->ioctl_count);
633 int mga_unlock(struct inode *inode, struct file *filp, unsigned int cmd,
636 drm_file_t *priv = filp->private_data;
637 drm_device_t *dev = priv->dev;
640 copy_from_user_ret(&lock, (drm_lock_t *)arg, sizeof(lock), -EFAULT);
642 if (lock.context == DRM_KERNEL_CONTEXT) {
643 DRM_ERROR("Process %d using kernel context %d\n",
644 current->pid, lock.context);
648 DRM_DEBUG("%d frees lock (%d holds)\n",
650 _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
651 atomic_inc(&dev->total_unlocks);
652 if (_DRM_LOCK_IS_CONT(dev->lock.hw_lock->lock))
653 atomic_inc(&dev->total_contends);
654 drm_lock_transfer(dev, &dev->lock.hw_lock->lock, DRM_KERNEL_CONTEXT);
655 mga_dma_schedule(dev, 1);
657 if (drm_lock_free(dev, &dev->lock.hw_lock->lock,
658 DRM_KERNEL_CONTEXT)) {