1 /* mga_drv.c -- Matrox g200/g400 driver -*- linux-c -*-
2  * Created: Mon Dec 13 01:56:22 1999 by jhartmann@precisioninsight.com
3  *
4  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
5  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
6  * All Rights Reserved.
7  *
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:
14  *
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
17  * Software.
18  *
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.
26  *
27  * Authors: Rickard E. (Rik) Faith <faith@valinux.com>
28  *	    Jeff Hartmann <jhartmann@valinux.com>
29  *
30  *
31  */
32 
33 #include <linux/config.h>
34 #include "drmP.h"
35 #include "mga_drv.h"
36 
37 #define MGA_NAME	 "mga"
38 #define MGA_DESC	 "Matrox G200/G400"
39 #define MGA_DATE	 "20000928"
40 #define MGA_MAJOR	 2
41 #define MGA_MINOR	 0
42 #define MGA_PATCHLEVEL	 1
43 
44 static drm_device_t	      mga_device;
45 drm_ctx_t		      mga_res_ctx;
46 
47 static struct file_operations mga_fops = {
48 #if LINUX_VERSION_CODE >= 0x020400
49 				/* This started being used during 2.4.0-test */
50 	owner:   THIS_MODULE,
51 #endif
52 	open:	 mga_open,
53 	flush:	 drm_flush,
54 	release: mga_release,
55 	ioctl:	 mga_ioctl,
56 	mmap:	 drm_mmap,
57 	read:	 drm_read,
58 	fasync:	 drm_fasync,
59    	poll:	 drm_poll,
60 };
61 
62 static struct miscdevice      mga_misc = {
63 	minor: MISC_DYNAMIC_MINOR,
64 	name:  MGA_NAME,
65 	fops:  &mga_fops,
66 };
67 
68 static drm_ioctl_desc_t	      mga_ioctls[] = {
69 	[DRM_IOCTL_NR(DRM_IOCTL_VERSION)]     = { mga_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 },
73 
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)]     = { mga_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)]    = { mga_addbufs,	  1, 1 },
81 	[DRM_IOCTL_NR(DRM_IOCTL_MARK_BUFS)]   = { mga_markbufs,	  1, 1 },
82 	[DRM_IOCTL_NR(DRM_IOCTL_INFO_BUFS)]   = { mga_infobufs,	  1, 0 },
83 	[DRM_IOCTL_NR(DRM_IOCTL_MAP_BUFS)]    = { mga_mapbufs,	  1, 0 },
84 	[DRM_IOCTL_NR(DRM_IOCTL_FREE_BUFS)]   = { mga_freebufs,	  1, 0 },
85 
86 	[DRM_IOCTL_NR(DRM_IOCTL_ADD_CTX)]     = { mga_addctx,	  1, 1 },
87 	[DRM_IOCTL_NR(DRM_IOCTL_RM_CTX)]      = { mga_rmctx,	  1, 1 },
88 	[DRM_IOCTL_NR(DRM_IOCTL_MOD_CTX)]     = { mga_modctx,	  1, 1 },
89 	[DRM_IOCTL_NR(DRM_IOCTL_GET_CTX)]     = { mga_getctx,	  1, 0 },
90 	[DRM_IOCTL_NR(DRM_IOCTL_SWITCH_CTX)]  = { mga_switchctx,  1, 1 },
91 	[DRM_IOCTL_NR(DRM_IOCTL_NEW_CTX)]     = { mga_newctx,	  1, 1 },
92 	[DRM_IOCTL_NR(DRM_IOCTL_RES_CTX)]     = { mga_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 },
95 
96 	[DRM_IOCTL_NR(DRM_IOCTL_DMA)]	      = { mga_dma,	  1, 0 },
97 
98 	[DRM_IOCTL_NR(DRM_IOCTL_LOCK)]	      = { mga_lock,	  1, 0 },
99 	[DRM_IOCTL_NR(DRM_IOCTL_UNLOCK)]      = { mga_unlock,	  1, 0 },
100 	[DRM_IOCTL_NR(DRM_IOCTL_FINISH)]      = { drm_finish,	  1, 0 },
101 
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_MGA_INIT)]    = { mga_dma_init,    1, 1 },
111    	[DRM_IOCTL_NR(DRM_IOCTL_MGA_SWAP)]    = { mga_swap_bufs,   1, 0 },
112    	[DRM_IOCTL_NR(DRM_IOCTL_MGA_CLEAR)]   = { mga_clear_bufs,  1, 0 },
113    	[DRM_IOCTL_NR(DRM_IOCTL_MGA_ILOAD)]   = { mga_iload,       1, 0 },
114    	[DRM_IOCTL_NR(DRM_IOCTL_MGA_VERTEX)]  = { mga_vertex,      1, 0 },
115    	[DRM_IOCTL_NR(DRM_IOCTL_MGA_FLUSH)]   = { mga_flush_ioctl, 1, 0 },
116    	[DRM_IOCTL_NR(DRM_IOCTL_MGA_INDICES)] = { mga_indices,     1, 0 },
117 };
118 
119 #define MGA_IOCTL_COUNT DRM_ARRAY_SIZE(mga_ioctls)
120 
121 #ifdef MODULE
122 static char		      *mga = NULL;
123 #endif
124 
125 MODULE_AUTHOR("VA Linux Systems, Inc.");
126 MODULE_DESCRIPTION("Matrox G200/G400");
127 MODULE_LICENSE("GPL and additional rights");
128 MODULE_PARM(mga, "s");
129 
130 #ifndef MODULE
131 /* mga_options is called by the kernel to parse command-line options passed
132  * via the boot-loader (e.g., LILO).  It calls the insmod option routine,
133  * drm_parse_drm.
134  */
135 
mga_options(char * str)136 static int __init mga_options(char *str)
137 {
138 	drm_parse_options(str);
139 	return 1;
140 }
141 
142 __setup("mga=", mga_options);
143 #endif
144 
mga_setup(drm_device_t * dev)145 static int mga_setup(drm_device_t *dev)
146 {
147 	int i;
148 
149 	atomic_set(&dev->ioctl_count, 0);
150 	atomic_set(&dev->vma_count, 0);
151 	dev->buf_use	  = 0;
152 	atomic_set(&dev->buf_alloc, 0);
153 
154 	drm_dma_setup(dev);
155 
156 	atomic_set(&dev->total_open, 0);
157 	atomic_set(&dev->total_close, 0);
158 	atomic_set(&dev->total_ioctl, 0);
159 	atomic_set(&dev->total_irq, 0);
160 	atomic_set(&dev->total_ctx, 0);
161 	atomic_set(&dev->total_locks, 0);
162 	atomic_set(&dev->total_unlocks, 0);
163 	atomic_set(&dev->total_contends, 0);
164 	atomic_set(&dev->total_sleeps, 0);
165 
166 	for (i = 0; i < DRM_HASH_SIZE; i++) {
167 		dev->magiclist[i].head = NULL;
168 		dev->magiclist[i].tail = NULL;
169 	}
170 	dev->maplist	    = NULL;
171 	dev->map_count	    = 0;
172 	dev->vmalist	    = NULL;
173 	dev->lock.hw_lock   = NULL;
174 	init_waitqueue_head(&dev->lock.lock_queue);
175 	dev->queue_count    = 0;
176 	dev->queue_reserved = 0;
177 	dev->queue_slots    = 0;
178 	dev->queuelist	    = NULL;
179 	dev->irq	    = 0;
180 	dev->context_flag   = 0;
181 	dev->interrupt_flag = 0;
182 	dev->dma_flag	    = 0;
183 	dev->last_context   = 0;
184 	dev->last_switch    = 0;
185 	dev->last_checked   = 0;
186 	init_timer(&dev->timer);
187 	init_waitqueue_head(&dev->context_wait);
188 
189 	dev->ctx_start	    = 0;
190 	dev->lck_start	    = 0;
191 
192 	dev->buf_rp	  = dev->buf;
193 	dev->buf_wp	  = dev->buf;
194 	dev->buf_end	  = dev->buf + DRM_BSZ;
195 	dev->buf_async	  = NULL;
196 	init_waitqueue_head(&dev->buf_readers);
197 	init_waitqueue_head(&dev->buf_writers);
198 
199 	DRM_DEBUG("\n");
200 
201 	/* The kernel's context could be created here, but is now created
202 	   in drm_dma_enqueue.	This is more resource-efficient for
203 	   hardware that does not do DMA, but may mean that
204 	   drm_select_queue fails between the time the interrupt is
205 	   initialized and the time the queues are initialized. */
206 
207 	return 0;
208 }
209 
210 
mga_takedown(drm_device_t * dev)211 static int mga_takedown(drm_device_t *dev)
212 {
213 	int		  i;
214 	drm_magic_entry_t *pt, *next;
215 	drm_map_t	  *map;
216 	drm_vma_entry_t	  *vma, *vma_next;
217 
218 	DRM_DEBUG("\n");
219 
220 	if (dev->dev_private) mga_dma_cleanup(dev);
221 	if (dev->irq) mga_irq_uninstall(dev);
222 
223 	down(&dev->struct_sem);
224 	del_timer(&dev->timer);
225 
226 	if (dev->devname) {
227 		drm_free(dev->devname, strlen(dev->devname)+1, DRM_MEM_DRIVER);
228 		dev->devname = NULL;
229 	}
230 
231 	if (dev->unique) {
232 		drm_free(dev->unique, strlen(dev->unique)+1, DRM_MEM_DRIVER);
233 		dev->unique = NULL;
234 		dev->unique_len = 0;
235 	}
236 				/* Clear pid list */
237 	for (i = 0; i < DRM_HASH_SIZE; i++) {
238 		for (pt = dev->magiclist[i].head; pt; pt = next) {
239 			next = pt->next;
240 			drm_free(pt, sizeof(*pt), DRM_MEM_MAGIC);
241 		}
242 		dev->magiclist[i].head = dev->magiclist[i].tail = NULL;
243 	}
244    				/* Clear AGP information */
245 	if (dev->agp) {
246 		drm_agp_mem_t *entry;
247 		drm_agp_mem_t *nexte;
248 
249 				/* Remove AGP resources, but leave dev->agp
250                                    intact until cleanup is called. */
251 		for (entry = dev->agp->memory; entry; entry = nexte) {
252 			nexte = entry->next;
253 			if (entry->bound) drm_unbind_agp(entry->memory);
254 			drm_free_agp(entry->memory, entry->pages);
255 			drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
256 		}
257 		dev->agp->memory = NULL;
258 
259 		if (dev->agp->acquired)	_drm_agp_release();
260 
261 		dev->agp->acquired = 0;
262 		dev->agp->enabled  = 0;
263 	}
264 				/* Clear vma list (only built for debugging) */
265 	if (dev->vmalist) {
266 		for (vma = dev->vmalist; vma; vma = vma_next) {
267 			vma_next = vma->next;
268 			drm_free(vma, sizeof(*vma), DRM_MEM_VMAS);
269 		}
270 		dev->vmalist = NULL;
271 	}
272 
273 				/* Clear map area and mtrr information */
274 	if (dev->maplist) {
275 		for (i = 0; i < dev->map_count; i++) {
276 			map = dev->maplist[i];
277 			switch (map->type) {
278 			case _DRM_REGISTERS:
279 			case _DRM_FRAME_BUFFER:
280 #ifdef CONFIG_MTRR
281 				if (map->mtrr >= 0) {
282 					int retcode;
283 					retcode = mtrr_del(map->mtrr,
284 							   map->offset,
285 							   map->size);
286 					DRM_DEBUG("mtrr_del = %d\n", retcode);
287 				}
288 #endif
289 				drm_ioremapfree(map->handle, map->size, dev);
290 				break;
291 			case _DRM_SHM:
292 				drm_free_pages((unsigned long)map->handle,
293 					       drm_order(map->size)
294 					       - PAGE_SHIFT,
295 					       DRM_MEM_SAREA);
296 				break;
297 			case _DRM_AGP:
298 				break;
299 			}
300 			drm_free(map, sizeof(*map), DRM_MEM_MAPS);
301 		}
302 		drm_free(dev->maplist,
303 			 dev->map_count * sizeof(*dev->maplist),
304 			 DRM_MEM_MAPS);
305 		dev->maplist   = NULL;
306 		dev->map_count = 0;
307 	}
308 
309 	if (dev->queuelist) {
310 		for (i = 0; i < dev->queue_count; i++) {
311 			drm_waitlist_destroy(&dev->queuelist[i]->waitlist);
312 			if (dev->queuelist[i]) {
313 				drm_free(dev->queuelist[i],
314 					 sizeof(*dev->queuelist[0]),
315 					 DRM_MEM_QUEUES);
316 				dev->queuelist[i] = NULL;
317 			}
318 		}
319 		drm_free(dev->queuelist,
320 			 dev->queue_slots * sizeof(*dev->queuelist),
321 			 DRM_MEM_QUEUES);
322 		dev->queuelist	 = NULL;
323 	}
324 
325 	drm_dma_takedown(dev);
326 
327 	dev->queue_count     = 0;
328 	if (dev->lock.hw_lock) {
329 		dev->lock.hw_lock    = NULL; /* SHM removed */
330 		dev->lock.pid	     = 0;
331 		wake_up_interruptible(&dev->lock.lock_queue);
332 	}
333 	up(&dev->struct_sem);
334 
335 	return 0;
336 }
337 
338 /* mga_init is called via init_module at module load time, or via
339  * linux/init/main.c (this is not currently supported). */
340 
mga_init(void)341 static int __init mga_init(void)
342 {
343 	int		      retcode;
344 	drm_device_t	      *dev = &mga_device;
345 
346 	DRM_DEBUG("\n");
347 
348 	memset((void *)dev, 0, sizeof(*dev));
349 	dev->count_lock	  = SPIN_LOCK_UNLOCKED;
350 	sema_init(&dev->struct_sem, 1);
351 
352 #ifdef MODULE
353 	drm_parse_options(mga);
354 #endif
355 	if ((retcode = misc_register(&mga_misc))) {
356 		DRM_ERROR("Cannot register \"%s\"\n", MGA_NAME);
357 		return retcode;
358 	}
359 	dev->device = MKDEV(MISC_MAJOR, mga_misc.minor);
360 	dev->name   = MGA_NAME;
361 
362 	drm_mem_init();
363 	drm_proc_init(dev);
364 	dev->agp    = drm_agp_init();
365       	if(dev->agp == NULL) {
366 	   	DRM_INFO("The mga drm module requires the agpgart module"
367 		         " to function correctly\nPlease load the agpgart"
368 		         " module before you load the mga module\n");
369 	   	drm_proc_cleanup();
370 	   	misc_deregister(&mga_misc);
371 	   	mga_takedown(dev);
372 	   	return -ENOMEM;
373 	}
374 #ifdef CONFIG_MTRR
375    	dev->agp->agp_mtrr = mtrr_add(dev->agp->agp_info.aper_base,
376 				      dev->agp->agp_info.aper_size * 1024 * 1024,
377 				      MTRR_TYPE_WRCOMB,
378 				      1);
379 #endif
380 	if((retcode = drm_ctxbitmap_init(dev))) {
381 		DRM_ERROR("Cannot allocate memory for context bitmap.\n");
382 		drm_proc_cleanup();
383 		misc_deregister(&mga_misc);
384 		mga_takedown(dev);
385 		return retcode;
386 	}
387 
388 	DRM_INFO("Initialized %s %d.%d.%d %s on minor %d\n",
389 		 MGA_NAME,
390 		 MGA_MAJOR,
391 		 MGA_MINOR,
392 		 MGA_PATCHLEVEL,
393 		 MGA_DATE,
394 		 mga_misc.minor);
395 
396 	return 0;
397 }
398 
399 /* mga_cleanup is called via cleanup_module at module unload time. */
400 
mga_cleanup(void)401 static void __exit mga_cleanup(void)
402 {
403 	drm_device_t	      *dev = &mga_device;
404 
405 	DRM_DEBUG("\n");
406 
407 	drm_proc_cleanup();
408 	if (misc_deregister(&mga_misc)) {
409 		DRM_ERROR("Cannot unload module\n");
410 	} else {
411 		DRM_INFO("Module unloaded\n");
412 	}
413 	drm_ctxbitmap_cleanup(dev);
414 #ifdef CONFIG_MTRR
415    	if(dev->agp && dev->agp->agp_mtrr) {
416 	   	int retval;
417 	   	retval = mtrr_del(dev->agp->agp_mtrr,
418 				  dev->agp->agp_info.aper_base,
419 				  dev->agp->agp_info.aper_size * 1024*1024);
420 	   	DRM_DEBUG("mtrr_del = %d\n", retval);
421 	}
422 #endif
423 
424 	mga_takedown(dev);
425 	if (dev->agp) {
426 		drm_agp_uninit();
427 		drm_free(dev->agp, sizeof(*dev->agp), DRM_MEM_AGPLISTS);
428 		dev->agp = NULL;
429 	}
430 }
431 
432 module_init(mga_init);
433 module_exit(mga_cleanup);
434 
435 
mga_version(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)436 int mga_version(struct inode *inode, struct file *filp, unsigned int cmd,
437 		  unsigned long arg)
438 {
439 	drm_version_t version;
440 	int	      len;
441 
442 	if (copy_from_user(&version,
443 			   (drm_version_t *)arg,
444 			   sizeof(version)))
445 		return -EFAULT;
446 
447 #define DRM_COPY(name,value)				     \
448 	len = strlen(value);				     \
449 	if (len > name##_len) len = name##_len;		     \
450 	name##_len = strlen(value);			     \
451 	if (len && name) {				     \
452 		if (copy_to_user(name, value, len))	     \
453 			return -EFAULT;			     \
454 	}
455 
456 	version.version_major	   = MGA_MAJOR;
457 	version.version_minor	   = MGA_MINOR;
458 	version.version_patchlevel = MGA_PATCHLEVEL;
459 
460 	DRM_COPY(version.name, MGA_NAME);
461 	DRM_COPY(version.date, MGA_DATE);
462 	DRM_COPY(version.desc, MGA_DESC);
463 
464 	if (copy_to_user((drm_version_t *)arg,
465 			 &version,
466 			 sizeof(version)))
467 		return -EFAULT;
468 	return 0;
469 }
470 
mga_open(struct inode * inode,struct file * filp)471 int mga_open(struct inode *inode, struct file *filp)
472 {
473 	drm_device_t  *dev    = &mga_device;
474 	int	      retcode = 0;
475 
476 	DRM_DEBUG("open_count = %d\n", dev->open_count);
477 	if (!(retcode = drm_open_helper(inode, filp, dev))) {
478 #if LINUX_VERSION_CODE < 0x020333
479 		MOD_INC_USE_COUNT; /* Needed before Linux 2.3.51 */
480 #endif
481 		atomic_inc(&dev->total_open);
482 		spin_lock(&dev->count_lock);
483 		if (!dev->open_count++) {
484 			spin_unlock(&dev->count_lock);
485 			return mga_setup(dev);
486 		}
487 		spin_unlock(&dev->count_lock);
488 	}
489 	return retcode;
490 }
491 
mga_release(struct inode * inode,struct file * filp)492 int mga_release(struct inode *inode, struct file *filp)
493 {
494 	drm_file_t    *priv   = filp->private_data;
495 	drm_device_t  *dev;
496 	int	      retcode = 0;
497 
498 	lock_kernel();
499 	dev    = priv->dev;
500 	DRM_DEBUG("pid = %d, device = 0x%x, open_count = %d\n",
501 		  current->pid, dev->device, dev->open_count);
502 
503 	if (dev->lock.hw_lock && _DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock)
504 	    && dev->lock.pid == current->pid) {
505 	      	mga_reclaim_buffers(dev, priv->pid);
506 		DRM_INFO("Process %d dead (ctx %d, d_s = 0x%02lx)\n",
507 			 current->pid,
508 			 _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock),
509 			 dev->dev_private ?
510 			 ((drm_mga_private_t *)dev->dev_private)
511 			 ->dispatch_status
512 			 : 0);
513 
514 		if (dev->dev_private)
515 			((drm_mga_private_t *)dev->dev_private)
516 				->dispatch_status &= MGA_IN_DISPATCH;
517 
518 		drm_lock_free(dev,
519 			      &dev->lock.hw_lock->lock,
520 			      _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
521 	} else if (dev->lock.hw_lock) {
522 	   	/* The lock is required to reclaim buffers */
523 	   	DECLARE_WAITQUEUE(entry, current);
524 	   	add_wait_queue(&dev->lock.lock_queue, &entry);
525 		for (;;) {
526 			current->state = TASK_INTERRUPTIBLE;
527 			if (!dev->lock.hw_lock) {
528 				/* Device has been unregistered */
529 				retcode = -EINTR;
530 				break;
531 			}
532 			if (drm_lock_take(&dev->lock.hw_lock->lock,
533 					  DRM_KERNEL_CONTEXT)) {
534 				dev->lock.pid	    = priv->pid;
535 				dev->lock.lock_time = jiffies;
536 				atomic_inc(&dev->total_locks);
537 				break;	/* Got lock */
538 			}
539 				/* Contention */
540 			atomic_inc(&dev->total_sleeps);
541 			schedule();
542 			if (signal_pending(current)) {
543 				retcode = -ERESTARTSYS;
544 				break;
545 			}
546 		}
547 		current->state = TASK_RUNNING;
548 		remove_wait_queue(&dev->lock.lock_queue, &entry);
549 	   	if(!retcode) {
550 		   	mga_reclaim_buffers(dev, priv->pid);
551 			if (dev->dev_private)
552 				((drm_mga_private_t *)dev->dev_private)
553 					->dispatch_status &= MGA_IN_DISPATCH;
554 		   	drm_lock_free(dev, &dev->lock.hw_lock->lock,
555 				      DRM_KERNEL_CONTEXT);
556 		}
557 	}
558 	drm_fasync(-1, filp, 0);
559 
560 	down(&dev->struct_sem);
561 	if (priv->remove_auth_on_close == 1) {
562 		drm_file_t *temp = dev->file_first;
563 		while(temp) {
564 			temp->authenticated = 0;
565 			temp = temp->next;
566 		}
567 	}
568 	if (priv->prev) priv->prev->next = priv->next;
569 	else		dev->file_first	 = priv->next;
570 	if (priv->next) priv->next->prev = priv->prev;
571 	else		dev->file_last	 = priv->prev;
572 	up(&dev->struct_sem);
573 
574 	drm_free(priv, sizeof(*priv), DRM_MEM_FILES);
575 #if LINUX_VERSION_CODE < 0x020333
576 	MOD_DEC_USE_COUNT; /* Needed before Linux 2.3.51 */
577 #endif
578    	atomic_inc(&dev->total_close);
579    	spin_lock(&dev->count_lock);
580    	if (!--dev->open_count) {
581 	   	if (atomic_read(&dev->ioctl_count) || dev->blocked) {
582 		   	DRM_ERROR("Device busy: %d %d\n",
583 				  atomic_read(&dev->ioctl_count),
584 				  dev->blocked);
585 		   	spin_unlock(&dev->count_lock);
586 			unlock_kernel();
587 		   	return -EBUSY;
588 		}
589 	   	spin_unlock(&dev->count_lock);
590 		unlock_kernel();
591 	   	return mga_takedown(dev);
592 	}
593 	spin_unlock(&dev->count_lock);
594 	unlock_kernel();
595 	return retcode;
596 }
597 
598 
599 /* drm_ioctl is called whenever a process performs an ioctl on /dev/drm. */
600 
mga_ioctl(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)601 int mga_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
602 		unsigned long arg)
603 {
604 	int		 nr	 = DRM_IOCTL_NR(cmd);
605 	drm_file_t	 *priv	 = filp->private_data;
606 	drm_device_t	 *dev	 = priv->dev;
607 	int		 retcode = 0;
608 	drm_ioctl_desc_t *ioctl;
609 	drm_ioctl_t	 *func;
610 
611 	atomic_inc(&dev->ioctl_count);
612 	atomic_inc(&dev->total_ioctl);
613 	++priv->ioctl_count;
614 
615 	if (nr >= MGA_IOCTL_COUNT) {
616 		retcode = -EINVAL;
617 	} else {
618 		ioctl	  = &mga_ioctls[nr];
619 		func	  = ioctl->func;
620 
621 		if (!func) {
622 			DRM_DEBUG("no function: pid = %d, cmd = 0x%02x,"
623 				  " nr = 0x%02x, dev 0x%x, auth = %d\n",
624 				  current->pid, cmd, nr, dev->device,
625 				  priv->authenticated);
626 			retcode = -EINVAL;
627 		} else if ((ioctl->root_only && !capable(CAP_SYS_ADMIN))
628 			    || (ioctl->auth_needed && !priv->authenticated)) {
629 			retcode = -EACCES;
630 		} else {
631 			retcode = (func)(inode, filp, cmd, arg);
632 		}
633 	}
634 
635 	atomic_dec(&dev->ioctl_count);
636 	return retcode;
637 }
638 
mga_unlock(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)639 int mga_unlock(struct inode *inode, struct file *filp, unsigned int cmd,
640 		 unsigned long arg)
641 {
642 	drm_file_t	  *priv	  = filp->private_data;
643 	drm_device_t	  *dev	  = priv->dev;
644 	drm_lock_t	  lock;
645 
646 	if (copy_from_user(&lock, (drm_lock_t *)arg, sizeof(lock)))
647 		return -EFAULT;
648 
649 	if (lock.context == DRM_KERNEL_CONTEXT) {
650 		DRM_ERROR("Process %d using kernel context %d\n",
651 			  current->pid, lock.context);
652 		return -EINVAL;
653 	}
654 
655 	atomic_inc(&dev->total_unlocks);
656 	if (_DRM_LOCK_IS_CONT(dev->lock.hw_lock->lock))
657 		atomic_inc(&dev->total_contends);
658 	drm_lock_transfer(dev, &dev->lock.hw_lock->lock, DRM_KERNEL_CONTEXT);
659 	mga_dma_schedule(dev, 1);
660 
661 	if (drm_lock_free(dev, &dev->lock.hw_lock->lock,
662 			  DRM_KERNEL_CONTEXT)) DRM_ERROR("\n");
663 
664 	unblock_all_signals();
665 	return 0;
666 }
667