1 /* gamma.c -- 3dlabs GMX 2000 driver -*- linux-c -*-
2 * Created: Mon Jan 4 08:58:31 1999 by faith@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.
28 * Rickard E. (Rik) Faith <faith@valinux.com>
32 #include <linux/config.h>
34 #include "gamma_drv.h"
36 #ifndef PCI_DEVICE_ID_3DLABS_GAMMA
37 #define PCI_DEVICE_ID_3DLABS_GAMMA 0x0008
39 #ifndef PCI_DEVICE_ID_3DLABS_MX
40 #define PCI_DEVICE_ID_3DLABS_MX 0x0006
43 #define GAMMA_NAME "gamma"
44 #define GAMMA_DESC "3dlabs GMX 2000"
45 #define GAMMA_DATE "20000910"
48 #define GAMMA_PATCHLEVEL 0
50 static drm_device_t gamma_device
;
52 static struct file_operations gamma_fops
= {
53 #if LINUX_VERSION_CODE >= 0x020400
54 /* This started being used during 2.4.0-test */
59 release
: gamma_release
,
67 static struct miscdevice gamma_misc
= {
68 minor
: MISC_DYNAMIC_MINOR
,
73 static drm_ioctl_desc_t gamma_ioctls
[] = {
74 [DRM_IOCTL_NR(DRM_IOCTL_VERSION
)] = { gamma_version
, 0, 0 },
75 [DRM_IOCTL_NR(DRM_IOCTL_GET_UNIQUE
)] = { drm_getunique
, 0, 0 },
76 [DRM_IOCTL_NR(DRM_IOCTL_GET_MAGIC
)] = { drm_getmagic
, 0, 0 },
77 [DRM_IOCTL_NR(DRM_IOCTL_IRQ_BUSID
)] = { drm_irq_busid
, 0, 1 },
79 [DRM_IOCTL_NR(DRM_IOCTL_SET_UNIQUE
)] = { drm_setunique
, 1, 1 },
80 [DRM_IOCTL_NR(DRM_IOCTL_BLOCK
)] = { drm_block
, 1, 1 },
81 [DRM_IOCTL_NR(DRM_IOCTL_UNBLOCK
)] = { drm_unblock
, 1, 1 },
82 [DRM_IOCTL_NR(DRM_IOCTL_CONTROL
)] = { gamma_control
, 1, 1 },
83 [DRM_IOCTL_NR(DRM_IOCTL_AUTH_MAGIC
)] = { drm_authmagic
, 1, 1 },
84 [DRM_IOCTL_NR(DRM_IOCTL_ADD_MAP
)] = { drm_addmap
, 1, 1 },
85 [DRM_IOCTL_NR(DRM_IOCTL_ADD_BUFS
)] = { drm_addbufs
, 1, 1 },
86 [DRM_IOCTL_NR(DRM_IOCTL_MARK_BUFS
)] = { drm_markbufs
, 1, 1 },
87 [DRM_IOCTL_NR(DRM_IOCTL_INFO_BUFS
)] = { drm_infobufs
, 1, 0 },
88 [DRM_IOCTL_NR(DRM_IOCTL_MAP_BUFS
)] = { drm_mapbufs
, 1, 0 },
89 [DRM_IOCTL_NR(DRM_IOCTL_FREE_BUFS
)] = { drm_freebufs
, 1, 0 },
91 [DRM_IOCTL_NR(DRM_IOCTL_ADD_CTX
)] = { drm_addctx
, 1, 1 },
92 [DRM_IOCTL_NR(DRM_IOCTL_RM_CTX
)] = { drm_rmctx
, 1, 1 },
93 [DRM_IOCTL_NR(DRM_IOCTL_MOD_CTX
)] = { drm_modctx
, 1, 1 },
94 [DRM_IOCTL_NR(DRM_IOCTL_GET_CTX
)] = { drm_getctx
, 1, 0 },
95 [DRM_IOCTL_NR(DRM_IOCTL_SWITCH_CTX
)] = { drm_switchctx
, 1, 1 },
96 [DRM_IOCTL_NR(DRM_IOCTL_NEW_CTX
)] = { drm_newctx
, 1, 1 },
97 [DRM_IOCTL_NR(DRM_IOCTL_RES_CTX
)] = { drm_resctx
, 1, 0 },
98 [DRM_IOCTL_NR(DRM_IOCTL_ADD_DRAW
)] = { drm_adddraw
, 1, 1 },
99 [DRM_IOCTL_NR(DRM_IOCTL_RM_DRAW
)] = { drm_rmdraw
, 1, 1 },
100 [DRM_IOCTL_NR(DRM_IOCTL_DMA
)] = { gamma_dma
, 1, 0 },
101 [DRM_IOCTL_NR(DRM_IOCTL_LOCK
)] = { gamma_lock
, 1, 0 },
102 [DRM_IOCTL_NR(DRM_IOCTL_UNLOCK
)] = { gamma_unlock
, 1, 0 },
103 [DRM_IOCTL_NR(DRM_IOCTL_FINISH
)] = { drm_finish
, 1, 0 },
105 #define GAMMA_IOCTL_COUNT DRM_ARRAY_SIZE(gamma_ioctls)
108 static char *gamma
= NULL
;
110 static int devices
= 0;
112 MODULE_AUTHOR("VA Linux Systems, Inc.");
113 MODULE_DESCRIPTION("3dlabs GMX 2000");
114 MODULE_PARM(gamma
, "s");
115 MODULE_PARM(devices
, "i");
116 MODULE_PARM_DESC(devices
,
117 "devices=x, where x is the number of MX chips on card\n");
119 /* gamma_options is called by the kernel to parse command-line options
120 * passed via the boot-loader (e.g., LILO). It calls the insmod option
121 * routine, drm_parse_options.
125 static int __init
gamma_options(char *str
)
127 drm_parse_options(str
);
131 __setup("gamma=", gamma_options
);
134 static int gamma_setup(drm_device_t
*dev
)
138 atomic_set(&dev
->ioctl_count
, 0);
139 atomic_set(&dev
->vma_count
, 0);
141 atomic_set(&dev
->buf_alloc
, 0);
145 atomic_set(&dev
->total_open
, 0);
146 atomic_set(&dev
->total_close
, 0);
147 atomic_set(&dev
->total_ioctl
, 0);
148 atomic_set(&dev
->total_irq
, 0);
149 atomic_set(&dev
->total_ctx
, 0);
150 atomic_set(&dev
->total_locks
, 0);
151 atomic_set(&dev
->total_unlocks
, 0);
152 atomic_set(&dev
->total_contends
, 0);
153 atomic_set(&dev
->total_sleeps
, 0);
155 for (i
= 0; i
< DRM_HASH_SIZE
; i
++) {
156 dev
->magiclist
[i
].head
= NULL
;
157 dev
->magiclist
[i
].tail
= NULL
;
162 dev
->lock
.hw_lock
= NULL
;
163 init_waitqueue_head(&dev
->lock
.lock_queue
);
164 dev
->queue_count
= 0;
165 dev
->queue_reserved
= 0;
166 dev
->queue_slots
= 0;
167 dev
->queuelist
= NULL
;
169 dev
->context_flag
= 0;
170 dev
->interrupt_flag
= 0;
172 dev
->last_context
= 0;
173 dev
->last_switch
= 0;
174 dev
->last_checked
= 0;
175 init_timer(&dev
->timer
);
176 init_waitqueue_head(&dev
->context_wait
);
178 memset(&dev
->histo
, 0, sizeof(dev
->histo
));
183 dev
->buf_rp
= dev
->buf
;
184 dev
->buf_wp
= dev
->buf
;
185 dev
->buf_end
= dev
->buf
+ DRM_BSZ
;
186 dev
->buf_async
= NULL
;
187 init_waitqueue_head(&dev
->buf_readers
);
188 init_waitqueue_head(&dev
->buf_writers
);
192 /* The kernel's context could be created here, but is now created
193 in drm_dma_enqueue. This is more resource-efficient for
194 hardware that does not do DMA, but may mean that
195 drm_select_queue fails between the time the interrupt is
196 initialized and the time the queues are initialized. */
202 static int gamma_takedown(drm_device_t
*dev
)
205 drm_magic_entry_t
*pt
, *next
;
207 drm_vma_entry_t
*vma
, *vma_next
;
211 if (dev
->irq
) gamma_irq_uninstall(dev
);
213 down(&dev
->struct_sem
);
214 del_timer(&dev
->timer
);
217 drm_free(dev
->devname
, strlen(dev
->devname
)+1, DRM_MEM_DRIVER
);
222 drm_free(dev
->unique
, strlen(dev
->unique
)+1, DRM_MEM_DRIVER
);
227 for (i
= 0; i
< DRM_HASH_SIZE
; i
++) {
228 for (pt
= dev
->magiclist
[i
].head
; pt
; pt
= next
) {
230 drm_free(pt
, sizeof(*pt
), DRM_MEM_MAGIC
);
232 dev
->magiclist
[i
].head
= dev
->magiclist
[i
].tail
= NULL
;
235 /* Clear vma list (only built for debugging) */
237 for (vma
= dev
->vmalist
; vma
; vma
= vma_next
) {
238 vma_next
= vma
->next
;
239 drm_free(vma
, sizeof(*vma
), DRM_MEM_VMAS
);
244 /* Clear map area and mtrr information */
246 for (i
= 0; i
< dev
->map_count
; i
++) {
247 map
= dev
->maplist
[i
];
250 case _DRM_FRAME_BUFFER
:
252 if (map
->mtrr
>= 0) {
254 retcode
= mtrr_del(map
->mtrr
,
257 DRM_DEBUG("mtrr_del = %d\n", retcode
);
260 drm_ioremapfree(map
->handle
, map
->size
);
263 drm_free_pages((unsigned long)map
->handle
,
269 /* Do nothing here, because this is all
270 handled in the AGP/GART driver. */
273 drm_free(map
, sizeof(*map
), DRM_MEM_MAPS
);
275 drm_free(dev
->maplist
,
276 dev
->map_count
* sizeof(*dev
->maplist
),
282 if (dev
->queuelist
) {
283 for (i
= 0; i
< dev
->queue_count
; i
++) {
284 drm_waitlist_destroy(&dev
->queuelist
[i
]->waitlist
);
285 if (dev
->queuelist
[i
]) {
286 drm_free(dev
->queuelist
[i
],
287 sizeof(*dev
->queuelist
[0]),
289 dev
->queuelist
[i
] = NULL
;
292 drm_free(dev
->queuelist
,
293 dev
->queue_slots
* sizeof(*dev
->queuelist
),
295 dev
->queuelist
= NULL
;
298 drm_dma_takedown(dev
);
300 dev
->queue_count
= 0;
301 if (dev
->lock
.hw_lock
) {
302 dev
->lock
.hw_lock
= NULL
; /* SHM removed */
304 wake_up_interruptible(&dev
->lock
.lock_queue
);
306 up(&dev
->struct_sem
);
311 int gamma_found(void)
316 int gamma_find_devices(void)
318 struct pci_dev
*d
= NULL
, *one
= NULL
, *two
= NULL
;
320 d
= pci_find_device(PCI_VENDOR_ID_3DLABS
,PCI_DEVICE_ID_3DLABS_GAMMA
,d
);
323 one
= pci_find_device(PCI_VENDOR_ID_3DLABS
,PCI_DEVICE_ID_3DLABS_MX
,d
);
326 /* Make sure it's on the same card, if not - no MX's found */
327 if (PCI_SLOT(d
->devfn
) != PCI_SLOT(one
->devfn
)) return 0;
329 two
= pci_find_device(PCI_VENDOR_ID_3DLABS
,PCI_DEVICE_ID_3DLABS_MX
,one
);
332 /* Make sure it's on the same card, if not - only 1 MX found */
333 if (PCI_SLOT(d
->devfn
) != PCI_SLOT(two
->devfn
)) return 1;
335 /* Two MX's found - we don't currently support more than 2 */
339 /* gamma_init is called via init_module at module load time, or via
340 * linux/init/main.c (this is not currently supported). */
342 static int __init
gamma_init(void)
345 drm_device_t
*dev
= &gamma_device
;
349 memset((void *)dev
, 0, sizeof(*dev
));
350 dev
->count_lock
= SPIN_LOCK_UNLOCKED
;
351 sema_init(&dev
->struct_sem
, 1);
354 drm_parse_options(gamma
);
356 devices
= gamma_find_devices();
357 if (devices
== 0) return -1;
359 if ((retcode
= misc_register(&gamma_misc
))) {
360 DRM_ERROR("Cannot register \"%s\"\n", GAMMA_NAME
);
363 dev
->device
= MKDEV(MISC_MAJOR
, gamma_misc
.minor
);
364 dev
->name
= GAMMA_NAME
;
369 DRM_INFO("Initialized %s %d.%d.%d %s on minor %d with %d MX devices\n",
381 /* gamma_cleanup is called via cleanup_module at module unload time. */
383 static void __exit
gamma_cleanup(void)
385 drm_device_t
*dev
= &gamma_device
;
390 if (misc_deregister(&gamma_misc
)) {
391 DRM_ERROR("Cannot unload module\n");
393 DRM_INFO("Module unloaded\n");
398 module_init(gamma_init
);
399 module_exit(gamma_cleanup
);
402 int gamma_version(struct inode
*inode
, struct file
*filp
, unsigned int cmd
,
405 drm_version_t version
;
408 if (copy_from_user(&version
,
409 (drm_version_t
*)arg
,
413 #define DRM_COPY(name,value) \
414 len = strlen(value); \
415 if (len > name##_len) len = name##_len; \
416 name##_len = strlen(value); \
418 if (copy_to_user(name, value, len)) \
422 version
.version_major
= GAMMA_MAJOR
;
423 version
.version_minor
= GAMMA_MINOR
;
424 version
.version_patchlevel
= GAMMA_PATCHLEVEL
;
426 DRM_COPY(version
.name
, GAMMA_NAME
);
427 DRM_COPY(version
.date
, GAMMA_DATE
);
428 DRM_COPY(version
.desc
, GAMMA_DESC
);
430 if (copy_to_user((drm_version_t
*)arg
,
437 int gamma_open(struct inode
*inode
, struct file
*filp
)
439 drm_device_t
*dev
= &gamma_device
;
442 DRM_DEBUG("open_count = %d\n", dev
->open_count
);
443 if (!(retcode
= drm_open_helper(inode
, filp
, dev
))) {
444 #if LINUX_VERSION_CODE < 0x020333
445 MOD_INC_USE_COUNT
; /* Needed before Linux 2.3.51 */
447 atomic_inc(&dev
->total_open
);
448 spin_lock(&dev
->count_lock
);
449 if (!dev
->open_count
++) {
450 spin_unlock(&dev
->count_lock
);
451 return gamma_setup(dev
);
453 spin_unlock(&dev
->count_lock
);
458 int gamma_release(struct inode
*inode
, struct file
*filp
)
460 drm_file_t
*priv
= filp
->private_data
;
467 DRM_DEBUG("open_count = %d\n", dev
->open_count
);
468 if (!(retcode
= drm_release(inode
, filp
))) {
469 #if LINUX_VERSION_CODE < 0x020333
470 MOD_DEC_USE_COUNT
; /* Needed before Linux 2.3.51 */
472 atomic_inc(&dev
->total_close
);
473 spin_lock(&dev
->count_lock
);
474 if (!--dev
->open_count
) {
475 if (atomic_read(&dev
->ioctl_count
) || dev
->blocked
) {
476 DRM_ERROR("Device busy: %d %d\n",
477 atomic_read(&dev
->ioctl_count
),
479 spin_unlock(&dev
->count_lock
);
483 spin_unlock(&dev
->count_lock
);
485 return gamma_takedown(dev
);
487 spin_unlock(&dev
->count_lock
);
493 /* drm_ioctl is called whenever a process performs an ioctl on /dev/drm. */
495 int gamma_ioctl(struct inode
*inode
, struct file
*filp
, unsigned int cmd
,
498 int nr
= DRM_IOCTL_NR(cmd
);
499 drm_file_t
*priv
= filp
->private_data
;
500 drm_device_t
*dev
= priv
->dev
;
502 drm_ioctl_desc_t
*ioctl
;
505 atomic_inc(&dev
->ioctl_count
);
506 atomic_inc(&dev
->total_ioctl
);
509 DRM_DEBUG("pid = %d, cmd = 0x%02x, nr = 0x%02x, dev 0x%x, auth = %d\n",
510 current
->pid
, cmd
, nr
, dev
->device
, priv
->authenticated
);
512 if (nr
>= GAMMA_IOCTL_COUNT
) {
515 ioctl
= &gamma_ioctls
[nr
];
519 DRM_DEBUG("no function\n");
521 } else if ((ioctl
->root_only
&& !capable(CAP_SYS_ADMIN
))
522 || (ioctl
->auth_needed
&& !priv
->authenticated
)) {
525 retcode
= (func
)(inode
, filp
, cmd
, arg
);
529 atomic_dec(&dev
->ioctl_count
);
534 int gamma_unlock(struct inode
*inode
, struct file
*filp
, unsigned int cmd
,
537 drm_file_t
*priv
= filp
->private_data
;
538 drm_device_t
*dev
= priv
->dev
;
541 if (copy_from_user(&lock
, (drm_lock_t
*)arg
, sizeof(lock
)))
544 if (lock
.context
== DRM_KERNEL_CONTEXT
) {
545 DRM_ERROR("Process %d using kernel context %d\n",
546 current
->pid
, lock
.context
);
550 DRM_DEBUG("%d frees lock (%d holds)\n",
552 _DRM_LOCKING_CONTEXT(dev
->lock
.hw_lock
->lock
));
553 atomic_inc(&dev
->total_unlocks
);
554 if (_DRM_LOCK_IS_CONT(dev
->lock
.hw_lock
->lock
))
555 atomic_inc(&dev
->total_contends
);
556 drm_lock_transfer(dev
, &dev
->lock
.hw_lock
->lock
, DRM_KERNEL_CONTEXT
);
557 gamma_dma_schedule(dev
, 1);
558 if (!dev
->context_flag
) {
559 if (drm_lock_free(dev
, &dev
->lock
.hw_lock
->lock
,
560 DRM_KERNEL_CONTEXT
)) {
564 #if DRM_DMA_HISTOGRAM
565 atomic_inc(&dev
->histo
.lhld
[drm_histogram_slot(get_cycles()
569 unblock_all_signals();