1 /* r128_drv.c -- ATI Rage 128 driver -*- linux-c -*-
2 * Created: Mon Dec 13 09:47:27 1999 by faith@precisioninsight.com
3 *
4 * Copyright 1999, 2000 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:
28 * Rickard E. (Rik) Faith <faith@valinux.com>
29 * Kevin E. Martin <martin@valinux.com>
30 * Gareth Hughes <gareth@valinux.com>
31 *
32 */
33
34 #include <linux/config.h>
35 #include "drmP.h"
36 #include "r128_drv.h"
37
38 #define R128_NAME "r128"
39 #define R128_DESC "ATI Rage 128"
40 #define R128_DATE "20001215"
41 #define R128_MAJOR 2
42 #define R128_MINOR 1
43 #define R128_PATCHLEVEL 2
44
45 static drm_device_t r128_device;
46 drm_ctx_t r128_res_ctx;
47
48 static struct file_operations r128_fops = {
49 #if LINUX_VERSION_CODE >= 0x020400
50 /* This started being used during 2.4.0-test */
51 owner: THIS_MODULE,
52 #endif
53 open: r128_open,
54 flush: drm_flush,
55 release: r128_release,
56 ioctl: r128_ioctl,
57 mmap: drm_mmap,
58 read: drm_read,
59 fasync: drm_fasync,
60 poll: drm_poll,
61 };
62
63 static struct miscdevice r128_misc = {
64 minor: MISC_DYNAMIC_MINOR,
65 name: R128_NAME,
66 fops: &r128_fops,
67 };
68
69 static drm_ioctl_desc_t r128_ioctls[] = {
70 [DRM_IOCTL_NR(DRM_IOCTL_VERSION)] = { r128_version, 0, 0 },
71 [DRM_IOCTL_NR(DRM_IOCTL_GET_UNIQUE)] = { drm_getunique, 0, 0 },
72 [DRM_IOCTL_NR(DRM_IOCTL_GET_MAGIC)] = { drm_getmagic, 0, 0 },
73 [DRM_IOCTL_NR(DRM_IOCTL_IRQ_BUSID)] = { drm_irq_busid, 0, 1 },
74
75 [DRM_IOCTL_NR(DRM_IOCTL_SET_UNIQUE)] = { drm_setunique, 1, 1 },
76 [DRM_IOCTL_NR(DRM_IOCTL_BLOCK)] = { drm_block, 1, 1 },
77 [DRM_IOCTL_NR(DRM_IOCTL_UNBLOCK)] = { drm_unblock, 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)] = { r128_addbufs, 1, 1 },
81 [DRM_IOCTL_NR(DRM_IOCTL_MARK_BUFS)] = { drm_markbufs, 1, 1 },
82 [DRM_IOCTL_NR(DRM_IOCTL_INFO_BUFS)] = { drm_infobufs, 1, 0 },
83 [DRM_IOCTL_NR(DRM_IOCTL_MAP_BUFS)] = { r128_mapbufs, 1, 0 },
84 [DRM_IOCTL_NR(DRM_IOCTL_FREE_BUFS)] = { drm_freebufs, 1, 0 },
85
86 [DRM_IOCTL_NR(DRM_IOCTL_ADD_CTX)] = { r128_addctx, 1, 1 },
87 [DRM_IOCTL_NR(DRM_IOCTL_RM_CTX)] = { r128_rmctx, 1, 1 },
88 [DRM_IOCTL_NR(DRM_IOCTL_MOD_CTX)] = { r128_modctx, 1, 1 },
89 [DRM_IOCTL_NR(DRM_IOCTL_GET_CTX)] = { r128_getctx, 1, 0 },
90 [DRM_IOCTL_NR(DRM_IOCTL_SWITCH_CTX)] = { r128_switchctx, 1, 1 },
91 [DRM_IOCTL_NR(DRM_IOCTL_NEW_CTX)] = { r128_newctx, 1, 1 },
92 [DRM_IOCTL_NR(DRM_IOCTL_RES_CTX)] = { r128_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 [DRM_IOCTL_NR(DRM_IOCTL_DMA)] = { r128_cce_buffers, 1, 0 },
96 [DRM_IOCTL_NR(DRM_IOCTL_LOCK)] = { r128_lock, 1, 0 },
97 [DRM_IOCTL_NR(DRM_IOCTL_UNLOCK)] = { r128_unlock, 1, 0 },
98 [DRM_IOCTL_NR(DRM_IOCTL_FINISH)] = { drm_finish, 1, 0 },
99
100 #if defined(CONFIG_AGP) || defined(CONFIG_AGP_MODULE)
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 #endif
110
111 [DRM_IOCTL_NR(DRM_IOCTL_R128_INIT)] = { r128_cce_init, 1, 1 },
112 [DRM_IOCTL_NR(DRM_IOCTL_R128_CCE_START)] = { r128_cce_start, 1, 1 },
113 [DRM_IOCTL_NR(DRM_IOCTL_R128_CCE_STOP)] = { r128_cce_stop, 1, 1 },
114 [DRM_IOCTL_NR(DRM_IOCTL_R128_CCE_RESET)] = { r128_cce_reset, 1, 1 },
115 [DRM_IOCTL_NR(DRM_IOCTL_R128_CCE_IDLE)] = { r128_cce_idle, 1, 0 },
116 [DRM_IOCTL_NR(DRM_IOCTL_R128_RESET)] = { r128_engine_reset, 1, 0 },
117 [DRM_IOCTL_NR(DRM_IOCTL_R128_SWAP)] = { r128_cce_swap, 1, 0 },
118 [DRM_IOCTL_NR(DRM_IOCTL_R128_CLEAR)] = { r128_cce_clear, 1, 0 },
119 [DRM_IOCTL_NR(DRM_IOCTL_R128_VERTEX)] = { r128_cce_vertex, 1, 0 },
120 [DRM_IOCTL_NR(DRM_IOCTL_R128_INDICES)] = { r128_cce_indices, 1, 0 },
121 [DRM_IOCTL_NR(DRM_IOCTL_R128_BLIT)] = { r128_cce_blit, 1, 0 },
122 [DRM_IOCTL_NR(DRM_IOCTL_R128_DEPTH)] = { r128_cce_depth, 1, 0 },
123 [DRM_IOCTL_NR(DRM_IOCTL_R128_STIPPLE)] = { r128_cce_stipple, 1, 0 },
124 [DRM_IOCTL_NR(DRM_IOCTL_R128_PACKET)] = { r128_cce_packet, 1, 0 },
125 };
126 #define R128_IOCTL_COUNT DRM_ARRAY_SIZE(r128_ioctls)
127
128 #ifdef MODULE
129 static char *r128 = NULL;
130 #endif
131
132 MODULE_AUTHOR("VA Linux Systems, Inc.");
133 MODULE_DESCRIPTION("r128");
134 MODULE_LICENSE("GPL and additional rights");
135 MODULE_PARM(r128, "s");
136
137 #ifndef MODULE
138 /* r128_options is called by the kernel to parse command-line options
139 * passed via the boot-loader (e.g., LILO). It calls the insmod option
140 * routine, drm_parse_drm.
141 */
142
r128_options(char * str)143 static int __init r128_options(char *str)
144 {
145 drm_parse_options(str);
146 return 1;
147 }
148
149 __setup("r128=", r128_options);
150 #endif
151
r128_setup(drm_device_t * dev)152 static int r128_setup(drm_device_t *dev)
153 {
154 int i;
155
156 atomic_set(&dev->ioctl_count, 0);
157 atomic_set(&dev->vma_count, 0);
158 dev->buf_use = 0;
159 atomic_set(&dev->buf_alloc, 0);
160
161 drm_dma_setup(dev);
162
163 atomic_set(&dev->total_open, 0);
164 atomic_set(&dev->total_close, 0);
165 atomic_set(&dev->total_ioctl, 0);
166 atomic_set(&dev->total_irq, 0);
167 atomic_set(&dev->total_ctx, 0);
168 atomic_set(&dev->total_locks, 0);
169 atomic_set(&dev->total_unlocks, 0);
170 atomic_set(&dev->total_contends, 0);
171 atomic_set(&dev->total_sleeps, 0);
172
173 for (i = 0; i < DRM_HASH_SIZE; i++) {
174 dev->magiclist[i].head = NULL;
175 dev->magiclist[i].tail = NULL;
176 }
177 dev->maplist = NULL;
178 dev->map_count = 0;
179 dev->vmalist = NULL;
180 dev->lock.hw_lock = NULL;
181 init_waitqueue_head(&dev->lock.lock_queue);
182 dev->queue_count = 0;
183 dev->queue_reserved = 0;
184 dev->queue_slots = 0;
185 dev->queuelist = NULL;
186 dev->irq = 0;
187 dev->context_flag = 0;
188 dev->interrupt_flag = 0;
189 dev->dma_flag = 0;
190 dev->last_context = 0;
191 dev->last_switch = 0;
192 dev->last_checked = 0;
193 init_timer(&dev->timer);
194 init_waitqueue_head(&dev->context_wait);
195
196 dev->ctx_start = 0;
197 dev->lck_start = 0;
198
199 dev->buf_rp = dev->buf;
200 dev->buf_wp = dev->buf;
201 dev->buf_end = dev->buf + DRM_BSZ;
202 dev->buf_async = NULL;
203 init_waitqueue_head(&dev->buf_readers);
204 init_waitqueue_head(&dev->buf_writers);
205
206 r128_res_ctx.handle=-1;
207
208 DRM_DEBUG("\n");
209
210 /* The kernel's context could be created here, but is now created
211 in drm_dma_enqueue. This is more resource-efficient for
212 hardware that does not do DMA, but may mean that
213 drm_select_queue fails between the time the interrupt is
214 initialized and the time the queues are initialized. */
215
216 return 0;
217 }
218
219
r128_takedown(drm_device_t * dev)220 static int r128_takedown(drm_device_t *dev)
221 {
222 int i;
223 drm_magic_entry_t *pt, *next;
224 drm_map_t *map;
225 drm_vma_entry_t *vma, *vma_next;
226
227 DRM_DEBUG("\n");
228
229 down(&dev->struct_sem);
230 del_timer(&dev->timer);
231
232 if (dev->devname) {
233 drm_free(dev->devname, strlen(dev->devname)+1, DRM_MEM_DRIVER);
234 dev->devname = NULL;
235 }
236
237 if (dev->unique) {
238 drm_free(dev->unique, strlen(dev->unique)+1, DRM_MEM_DRIVER);
239 dev->unique = NULL;
240 dev->unique_len = 0;
241 }
242 /* Clear pid list */
243 for (i = 0; i < DRM_HASH_SIZE; i++) {
244 for (pt = dev->magiclist[i].head; pt; pt = next) {
245 next = pt->next;
246 drm_free(pt, sizeof(*pt), DRM_MEM_MAGIC);
247 }
248 dev->magiclist[i].head = dev->magiclist[i].tail = NULL;
249 }
250
251 #if defined(CONFIG_AGP) || defined(CONFIG_AGP_MODULE)
252 /* Clear AGP information */
253 if (dev->agp) {
254 drm_agp_mem_t *entry;
255 drm_agp_mem_t *nexte;
256
257 /* Remove AGP resources, but leave dev->agp
258 intact until r128_cleanup is called. */
259 for (entry = dev->agp->memory; entry; entry = nexte) {
260 nexte = entry->next;
261 if (entry->bound) drm_unbind_agp(entry->memory);
262 drm_free_agp(entry->memory, entry->pages);
263 drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS);
264 }
265 dev->agp->memory = NULL;
266
267 if (dev->agp->acquired) _drm_agp_release();
268
269 dev->agp->acquired = 0;
270 dev->agp->enabled = 0;
271 }
272 #endif
273
274 /* Clear vma list (only built for debugging) */
275 if (dev->vmalist) {
276 for (vma = dev->vmalist; vma; vma = vma_next) {
277 vma_next = vma->next;
278 drm_free(vma, sizeof(*vma), DRM_MEM_VMAS);
279 }
280 dev->vmalist = NULL;
281 }
282
283 /* Clear map area and mtrr information */
284 if (dev->maplist) {
285 for (i = 0; i < dev->map_count; i++) {
286 map = dev->maplist[i];
287 switch (map->type) {
288 case _DRM_REGISTERS:
289 case _DRM_FRAME_BUFFER:
290 #ifdef CONFIG_MTRR
291 if (map->mtrr >= 0) {
292 int retcode;
293 retcode = mtrr_del(map->mtrr,
294 map->offset,
295 map->size);
296 DRM_DEBUG("mtrr_del = %d\n", retcode);
297 }
298 #endif
299 drm_ioremapfree(map->handle, map->size, dev);
300 break;
301 case _DRM_SHM:
302 drm_free_pages((unsigned long)map->handle,
303 drm_order(map->size)
304 - PAGE_SHIFT,
305 DRM_MEM_SAREA);
306 break;
307 case _DRM_AGP:
308 /* Do nothing here, because this is all
309 handled in the AGP/GART driver. */
310 break;
311 }
312 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
313 }
314 drm_free(dev->maplist,
315 dev->map_count * sizeof(*dev->maplist),
316 DRM_MEM_MAPS);
317 dev->maplist = NULL;
318 dev->map_count = 0;
319 }
320
321 drm_dma_takedown(dev);
322
323 dev->queue_count = 0;
324 if (dev->lock.hw_lock) {
325 dev->lock.hw_lock = NULL; /* SHM removed */
326 dev->lock.pid = 0;
327 wake_up_interruptible(&dev->lock.lock_queue);
328 }
329 up(&dev->struct_sem);
330
331 return 0;
332 }
333
334 /* r128_init is called via init_module at module load time, or via
335 * linux/init/main.c (this is not currently supported). */
336
r128_init(void)337 static int __init r128_init(void)
338 {
339 int retcode;
340 drm_device_t *dev = &r128_device;
341
342 DRM_DEBUG("\n");
343
344 memset((void *)dev, 0, sizeof(*dev));
345 dev->count_lock = SPIN_LOCK_UNLOCKED;
346 sema_init(&dev->struct_sem, 1);
347
348 #ifdef MODULE
349 drm_parse_options(r128);
350 #endif
351
352 if ((retcode = misc_register(&r128_misc))) {
353 DRM_ERROR("Cannot register \"%s\"\n", R128_NAME);
354 return retcode;
355 }
356 dev->device = MKDEV(MISC_MAJOR, r128_misc.minor);
357 dev->name = R128_NAME;
358
359 drm_mem_init();
360 drm_proc_init(dev);
361
362 #if defined(CONFIG_AGP) || defined(CONFIG_AGP_MODULE)
363 dev->agp = drm_agp_init();
364 if (dev->agp == NULL) {
365 DRM_ERROR("Cannot initialize agpgart module.\n");
366 drm_proc_cleanup();
367 misc_deregister(&r128_misc);
368 r128_takedown(dev);
369 return -ENOMEM;
370 }
371
372 #ifdef CONFIG_MTRR
373 dev->agp->agp_mtrr = mtrr_add(dev->agp->agp_info.aper_base,
374 dev->agp->agp_info.aper_size*1024*1024,
375 MTRR_TYPE_WRCOMB,
376 1);
377 #endif
378 #endif
379
380 if((retcode = drm_ctxbitmap_init(dev))) {
381 DRM_ERROR("Cannot allocate memory for context bitmap.\n");
382 drm_proc_cleanup();
383 misc_deregister(&r128_misc);
384 r128_takedown(dev);
385 return retcode;
386 }
387
388 DRM_INFO("Initialized %s %d.%d.%d %s on minor %d\n",
389 R128_NAME,
390 R128_MAJOR,
391 R128_MINOR,
392 R128_PATCHLEVEL,
393 R128_DATE,
394 r128_misc.minor);
395
396 return 0;
397 }
398
399 /* r128_cleanup is called via cleanup_module at module unload time. */
400
r128_cleanup(void)401 static void __exit r128_cleanup(void)
402 {
403 drm_device_t *dev = &r128_device;
404
405 DRM_DEBUG("\n");
406
407 drm_proc_cleanup();
408 if (misc_deregister(&r128_misc)) {
409 DRM_ERROR("Cannot unload module\n");
410 } else {
411 DRM_INFO("Module unloaded\n");
412 }
413 drm_ctxbitmap_cleanup(dev);
414 r128_takedown(dev);
415 #if defined(CONFIG_AGP) || defined(CONFIG_AGP_MODULE)
416 if (dev->agp) {
417 drm_agp_uninit();
418 drm_free(dev->agp, sizeof(*dev->agp), DRM_MEM_AGPLISTS);
419 dev->agp = NULL;
420 }
421 #endif
422 }
423
424 module_init(r128_init);
425 module_exit(r128_cleanup);
426
427
r128_version(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)428 int r128_version(struct inode *inode, struct file *filp,
429 unsigned int cmd, unsigned long arg)
430 {
431 drm_version_t version;
432 int len;
433
434 if (copy_from_user(&version,
435 (drm_version_t *)arg,
436 sizeof(version)))
437 return -EFAULT;
438
439 #define DRM_COPY(name,value) \
440 len = strlen(value); \
441 if (len > name##_len) len = name##_len; \
442 name##_len = strlen(value); \
443 if (len && name) { \
444 if (copy_to_user(name, value, len)) \
445 return -EFAULT; \
446 }
447
448 version.version_major = R128_MAJOR;
449 version.version_minor = R128_MINOR;
450 version.version_patchlevel = R128_PATCHLEVEL;
451
452 DRM_COPY(version.name, R128_NAME);
453 DRM_COPY(version.date, R128_DATE);
454 DRM_COPY(version.desc, R128_DESC);
455
456 if (copy_to_user((drm_version_t *)arg,
457 &version,
458 sizeof(version)))
459 return -EFAULT;
460 return 0;
461 }
462
r128_open(struct inode * inode,struct file * filp)463 int r128_open(struct inode *inode, struct file *filp)
464 {
465 drm_device_t *dev = &r128_device;
466 int retcode = 0;
467
468 DRM_DEBUG("open_count = %d\n", dev->open_count);
469 if (!(retcode = drm_open_helper(inode, filp, dev))) {
470 #if LINUX_VERSION_CODE < 0x020333
471 MOD_INC_USE_COUNT; /* Needed before Linux 2.3.51 */
472 #endif
473 atomic_inc(&dev->total_open);
474 spin_lock(&dev->count_lock);
475 if (!dev->open_count++) {
476 spin_unlock(&dev->count_lock);
477 return r128_setup(dev);
478 }
479 spin_unlock(&dev->count_lock);
480 }
481
482 return retcode;
483 }
484
r128_release(struct inode * inode,struct file * filp)485 int r128_release(struct inode *inode, struct file *filp)
486 {
487 drm_file_t *priv = filp->private_data;
488 drm_device_t *dev;
489 int retcode = 0;
490
491 lock_kernel();
492 dev = priv->dev;
493 DRM_DEBUG("open_count = %d\n", dev->open_count);
494 if (!(retcode = drm_release(inode, filp))) {
495 #if LINUX_VERSION_CODE < 0x020333
496 MOD_DEC_USE_COUNT; /* Needed before Linux 2.3.51 */
497 #endif
498 atomic_inc(&dev->total_close);
499 spin_lock(&dev->count_lock);
500 if (!--dev->open_count) {
501 if (atomic_read(&dev->ioctl_count) || dev->blocked) {
502 DRM_ERROR("Device busy: %d %d\n",
503 atomic_read(&dev->ioctl_count),
504 dev->blocked);
505 spin_unlock(&dev->count_lock);
506 unlock_kernel();
507 return -EBUSY;
508 }
509 spin_unlock(&dev->count_lock);
510 unlock_kernel();
511 return r128_takedown(dev);
512 }
513 spin_unlock(&dev->count_lock);
514 }
515
516 unlock_kernel();
517 return retcode;
518 }
519
520 /* r128_ioctl is called whenever a process performs an ioctl on /dev/drm. */
r128_ioctl(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)521 int r128_ioctl(struct inode *inode, struct file *filp,
522 unsigned int cmd, unsigned long arg)
523 {
524 int nr = DRM_IOCTL_NR(cmd);
525 drm_file_t *priv = filp->private_data;
526 drm_device_t *dev = priv->dev;
527 int retcode = 0;
528 drm_ioctl_desc_t *ioctl;
529 drm_ioctl_t *func;
530
531 atomic_inc(&dev->ioctl_count);
532 atomic_inc(&dev->total_ioctl);
533 ++priv->ioctl_count;
534
535 DRM_DEBUG("pid = %d, cmd = 0x%02x, nr = 0x%02x, dev 0x%x, auth = %d\n",
536 current->pid, cmd, nr, dev->device, priv->authenticated);
537
538 if (nr >= R128_IOCTL_COUNT) {
539 retcode = -EINVAL;
540 } else {
541 ioctl = &r128_ioctls[nr];
542 func = ioctl->func;
543
544 if (!func) {
545 DRM_DEBUG("no function\n");
546 retcode = -EINVAL;
547 } else if ((ioctl->root_only && !capable(CAP_SYS_ADMIN))
548 || (ioctl->auth_needed && !priv->authenticated)) {
549 retcode = -EACCES;
550 } else {
551 retcode = (func)(inode, filp, cmd, arg);
552 }
553 }
554
555 #if 0
556 if ( retcode ) {
557 DRM_INFO( "%s 0x%x ret = %d\n", __FUNCTION__, nr, retcode );
558 }
559 #endif
560
561 atomic_dec(&dev->ioctl_count);
562 return retcode;
563 }
564
r128_lock(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)565 int r128_lock(struct inode *inode, struct file *filp,
566 unsigned int cmd, unsigned long arg)
567 {
568 drm_file_t *priv = filp->private_data;
569 drm_device_t *dev = priv->dev;
570 DECLARE_WAITQUEUE(entry, current);
571 int ret = 0;
572 drm_lock_t lock;
573 #if DRM_DMA_HISTOGRAM
574 cycles_t start;
575
576 dev->lck_start = start = get_cycles();
577 #endif
578
579 if (copy_from_user(&lock, (drm_lock_t *)arg, sizeof(lock)))
580 return -EFAULT;
581
582 if (lock.context == DRM_KERNEL_CONTEXT) {
583 DRM_ERROR("Process %d using kernel context %d\n",
584 current->pid, lock.context);
585 return -EINVAL;
586 }
587
588 DRM_DEBUG("%d (pid %d) requests lock (0x%08x), flags = 0x%08x\n",
589 lock.context, current->pid, dev->lock.hw_lock->lock,
590 lock.flags);
591
592 if (lock.context < 0)
593 return -EINVAL;
594
595 if (!ret) {
596 add_wait_queue(&dev->lock.lock_queue, &entry);
597 for (;;) {
598 current->state = TASK_INTERRUPTIBLE;
599 if (!dev->lock.hw_lock) {
600 /* Device has been unregistered */
601 ret = -EINTR;
602 break;
603 }
604 if (drm_lock_take(&dev->lock.hw_lock->lock,
605 lock.context)) {
606 dev->lock.pid = current->pid;
607 dev->lock.lock_time = jiffies;
608 atomic_inc(&dev->total_locks);
609 break; /* Got lock */
610 }
611
612 /* Contention */
613 atomic_inc(&dev->total_sleeps);
614 schedule();
615 if (signal_pending(current)) {
616 ret = -ERESTARTSYS;
617 break;
618 }
619 }
620 current->state = TASK_RUNNING;
621 remove_wait_queue(&dev->lock.lock_queue, &entry);
622 }
623
624 if (!ret) {
625 sigemptyset(&dev->sigmask);
626 sigaddset(&dev->sigmask, SIGSTOP);
627 sigaddset(&dev->sigmask, SIGTSTP);
628 sigaddset(&dev->sigmask, SIGTTIN);
629 sigaddset(&dev->sigmask, SIGTTOU);
630 dev->sigdata.context = lock.context;
631 dev->sigdata.lock = dev->lock.hw_lock;
632 block_all_signals(drm_notifier, &dev->sigdata, &dev->sigmask);
633 if (lock.flags & _DRM_LOCK_READY) {
634 /* Wait for space in DMA/FIFO */
635 }
636 if (lock.flags & _DRM_LOCK_QUIESCENT) {
637 /* Make hardware quiescent */
638 DRM_DEBUG( "not quiescent!\n" );
639 #if 0
640 r128_quiescent(dev);
641 #endif
642 }
643 }
644
645 #if LINUX_VERSION_CODE < 0x020400
646 if (lock.context != r128_res_ctx.handle) {
647 current->counter = 5;
648 current->priority = DEF_PRIORITY/4;
649 }
650 #endif
651 DRM_DEBUG("%d %s\n", lock.context, ret ? "interrupted" : "has lock");
652
653 #if DRM_DMA_HISTOGRAM
654 atomic_inc(&dev->histo.lacq[drm_histogram_slot(get_cycles() - start)]);
655 #endif
656
657 return ret;
658 }
659
660
r128_unlock(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)661 int r128_unlock(struct inode *inode, struct file *filp,
662 unsigned int cmd, unsigned long arg)
663 {
664 drm_file_t *priv = filp->private_data;
665 drm_device_t *dev = priv->dev;
666 drm_lock_t lock;
667
668 if (copy_from_user(&lock, (drm_lock_t *)arg, sizeof(lock)))
669 return -EFAULT;
670
671 if (lock.context == DRM_KERNEL_CONTEXT) {
672 DRM_ERROR("Process %d using kernel context %d\n",
673 current->pid, lock.context);
674 return -EINVAL;
675 }
676
677 DRM_DEBUG("%d frees lock (%d holds)\n",
678 lock.context,
679 _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
680 atomic_inc(&dev->total_unlocks);
681 if (_DRM_LOCK_IS_CONT(dev->lock.hw_lock->lock))
682 atomic_inc(&dev->total_contends);
683 drm_lock_transfer(dev, &dev->lock.hw_lock->lock, DRM_KERNEL_CONTEXT);
684 /* FIXME: Try to send data to card here */
685 if (!dev->context_flag) {
686 if (drm_lock_free(dev, &dev->lock.hw_lock->lock,
687 DRM_KERNEL_CONTEXT)) {
688 DRM_ERROR("\n");
689 }
690 }
691
692 #if LINUX_VERSION_CODE < 0x020400
693 if (lock.context != r128_res_ctx.handle) {
694 current->counter = 5;
695 current->priority = DEF_PRIORITY;
696 }
697 #endif
698 unblock_all_signals();
699 return 0;
700 }
701