]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/gpu/drm/drm_fops.c
drm: export drm_global_mutex for drivers to use
[net-next-2.6.git] / drivers / gpu / drm / drm_fops.c
1 /**
2  * \file drm_fops.c
3  * File operations for DRM
4  *
5  * \author Rickard E. (Rik) Faith <faith@valinux.com>
6  * \author Daryll Strauss <daryll@valinux.com>
7  * \author Gareth Hughes <gareth@valinux.com>
8  */
9
10 /*
11  * Created: Mon Jan  4 08:58:31 1999 by faith@valinux.com
12  *
13  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
14  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
15  * All Rights Reserved.
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining a
18  * copy of this software and associated documentation files (the "Software"),
19  * to deal in the Software without restriction, including without limitation
20  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
21  * and/or sell copies of the Software, and to permit persons to whom the
22  * Software is furnished to do so, subject to the following conditions:
23  *
24  * The above copyright notice and this permission notice (including the next
25  * paragraph) shall be included in all copies or substantial portions of the
26  * Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
31  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
32  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
33  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
34  * OTHER DEALINGS IN THE SOFTWARE.
35  */
36
37 #include "drmP.h"
38 #include <linux/poll.h>
39 #include <linux/slab.h>
40 #include <linux/smp_lock.h>
41
42 /* from BKL pushdown: note that nothing else serializes idr_find() */
43 DEFINE_MUTEX(drm_global_mutex);
44 EXPORT_SYMBOL(drm_global_mutex);
45
46 static int drm_open_helper(struct inode *inode, struct file *filp,
47                            struct drm_device * dev);
48
49 static int drm_setup(struct drm_device * dev)
50 {
51         int i;
52         int ret;
53
54         if (dev->driver->firstopen) {
55                 ret = dev->driver->firstopen(dev);
56                 if (ret != 0)
57                         return ret;
58         }
59
60         atomic_set(&dev->ioctl_count, 0);
61         atomic_set(&dev->vma_count, 0);
62
63         if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
64             !drm_core_check_feature(dev, DRIVER_MODESET)) {
65                 dev->buf_use = 0;
66                 atomic_set(&dev->buf_alloc, 0);
67
68                 i = drm_dma_setup(dev);
69                 if (i < 0)
70                         return i;
71         }
72
73         for (i = 0; i < ARRAY_SIZE(dev->counts); i++)
74                 atomic_set(&dev->counts[i], 0);
75
76         dev->sigdata.lock = NULL;
77
78         dev->queue_count = 0;
79         dev->queue_reserved = 0;
80         dev->queue_slots = 0;
81         dev->queuelist = NULL;
82         dev->context_flag = 0;
83         dev->interrupt_flag = 0;
84         dev->dma_flag = 0;
85         dev->last_context = 0;
86         dev->last_switch = 0;
87         dev->last_checked = 0;
88         init_waitqueue_head(&dev->context_wait);
89         dev->if_version = 0;
90
91         dev->ctx_start = 0;
92         dev->lck_start = 0;
93
94         dev->buf_async = NULL;
95         init_waitqueue_head(&dev->buf_readers);
96         init_waitqueue_head(&dev->buf_writers);
97
98         DRM_DEBUG("\n");
99
100         /*
101          * The kernel's context could be created here, but is now created
102          * in drm_dma_enqueue.  This is more resource-efficient for
103          * hardware that does not do DMA, but may mean that
104          * drm_select_queue fails between the time the interrupt is
105          * initialized and the time the queues are initialized.
106          */
107
108         return 0;
109 }
110
111 /**
112  * Open file.
113  *
114  * \param inode device inode
115  * \param filp file pointer.
116  * \return zero on success or a negative number on failure.
117  *
118  * Searches the DRM device with the same minor number, calls open_helper(), and
119  * increments the device open count. If the open count was previous at zero,
120  * i.e., it's the first that the device is open, then calls setup().
121  */
122 int drm_open(struct inode *inode, struct file *filp)
123 {
124         struct drm_device *dev = NULL;
125         int minor_id = iminor(inode);
126         struct drm_minor *minor;
127         int retcode = 0;
128
129         minor = idr_find(&drm_minors_idr, minor_id);
130         if (!minor)
131                 return -ENODEV;
132
133         if (!(dev = minor->dev))
134                 return -ENODEV;
135
136         retcode = drm_open_helper(inode, filp, dev);
137         if (!retcode) {
138                 atomic_inc(&dev->counts[_DRM_STAT_OPENS]);
139                 spin_lock(&dev->count_lock);
140                 if (!dev->open_count++) {
141                         spin_unlock(&dev->count_lock);
142                         retcode = drm_setup(dev);
143                         goto out;
144                 }
145                 spin_unlock(&dev->count_lock);
146         }
147 out:
148         if (!retcode) {
149                 mutex_lock(&dev->struct_mutex);
150                 if (minor->type == DRM_MINOR_LEGACY) {
151                         if (dev->dev_mapping == NULL)
152                                 dev->dev_mapping = inode->i_mapping;
153                         else if (dev->dev_mapping != inode->i_mapping)
154                                 retcode = -ENODEV;
155                 }
156                 mutex_unlock(&dev->struct_mutex);
157         }
158
159         return retcode;
160 }
161 EXPORT_SYMBOL(drm_open);
162
163 /**
164  * File \c open operation.
165  *
166  * \param inode device inode.
167  * \param filp file pointer.
168  *
169  * Puts the dev->fops corresponding to the device minor number into
170  * \p filp, call the \c open method, and restore the file operations.
171  */
172 int drm_stub_open(struct inode *inode, struct file *filp)
173 {
174         struct drm_device *dev = NULL;
175         struct drm_minor *minor;
176         int minor_id = iminor(inode);
177         int err = -ENODEV;
178         const struct file_operations *old_fops;
179
180         DRM_DEBUG("\n");
181
182         mutex_lock(&drm_global_mutex);
183         minor = idr_find(&drm_minors_idr, minor_id);
184         if (!minor)
185                 goto out;
186
187         if (!(dev = minor->dev))
188                 goto out;
189
190         old_fops = filp->f_op;
191         filp->f_op = fops_get(&dev->driver->fops);
192         if (filp->f_op == NULL) {
193                 filp->f_op = old_fops;
194                 goto out;
195         }
196         if (filp->f_op->open && (err = filp->f_op->open(inode, filp))) {
197                 fops_put(filp->f_op);
198                 filp->f_op = fops_get(old_fops);
199         }
200         fops_put(old_fops);
201
202 out:
203         mutex_unlock(&drm_global_mutex);
204         return err;
205 }
206
207 /**
208  * Check whether DRI will run on this CPU.
209  *
210  * \return non-zero if the DRI will run on this CPU, or zero otherwise.
211  */
212 static int drm_cpu_valid(void)
213 {
214 #if defined(__i386__)
215         if (boot_cpu_data.x86 == 3)
216                 return 0;       /* No cmpxchg on a 386 */
217 #endif
218 #if defined(__sparc__) && !defined(__sparc_v9__)
219         return 0;               /* No cmpxchg before v9 sparc. */
220 #endif
221         return 1;
222 }
223
224 /**
225  * Called whenever a process opens /dev/drm.
226  *
227  * \param inode device inode.
228  * \param filp file pointer.
229  * \param dev device.
230  * \return zero on success or a negative number on failure.
231  *
232  * Creates and initializes a drm_file structure for the file private data in \p
233  * filp and add it into the double linked list in \p dev.
234  */
235 static int drm_open_helper(struct inode *inode, struct file *filp,
236                            struct drm_device * dev)
237 {
238         int minor_id = iminor(inode);
239         struct drm_file *priv;
240         int ret;
241
242         if (filp->f_flags & O_EXCL)
243                 return -EBUSY;  /* No exclusive opens */
244         if (!drm_cpu_valid())
245                 return -EINVAL;
246
247         DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current), minor_id);
248
249         priv = kzalloc(sizeof(*priv), GFP_KERNEL);
250         if (!priv)
251                 return -ENOMEM;
252
253         filp->private_data = priv;
254         priv->filp = filp;
255         priv->uid = current_euid();
256         priv->pid = task_pid_nr(current);
257         priv->minor = idr_find(&drm_minors_idr, minor_id);
258         priv->ioctl_count = 0;
259         /* for compatibility root is always authenticated */
260         priv->authenticated = capable(CAP_SYS_ADMIN);
261         priv->lock_count = 0;
262
263         INIT_LIST_HEAD(&priv->lhead);
264         INIT_LIST_HEAD(&priv->fbs);
265         INIT_LIST_HEAD(&priv->event_list);
266         init_waitqueue_head(&priv->event_wait);
267         priv->event_space = 4096; /* set aside 4k for event buffer */
268
269         if (dev->driver->driver_features & DRIVER_GEM)
270                 drm_gem_open(dev, priv);
271
272         if (dev->driver->open) {
273                 ret = dev->driver->open(dev, priv);
274                 if (ret < 0)
275                         goto out_free;
276         }
277
278
279         /* if there is no current master make this fd it */
280         mutex_lock(&dev->struct_mutex);
281         if (!priv->minor->master) {
282                 /* create a new master */
283                 priv->minor->master = drm_master_create(priv->minor);
284                 if (!priv->minor->master) {
285                         mutex_unlock(&dev->struct_mutex);
286                         ret = -ENOMEM;
287                         goto out_free;
288                 }
289
290                 priv->is_master = 1;
291                 /* take another reference for the copy in the local file priv */
292                 priv->master = drm_master_get(priv->minor->master);
293
294                 priv->authenticated = 1;
295
296                 mutex_unlock(&dev->struct_mutex);
297                 if (dev->driver->master_create) {
298                         ret = dev->driver->master_create(dev, priv->master);
299                         if (ret) {
300                                 mutex_lock(&dev->struct_mutex);
301                                 /* drop both references if this fails */
302                                 drm_master_put(&priv->minor->master);
303                                 drm_master_put(&priv->master);
304                                 mutex_unlock(&dev->struct_mutex);
305                                 goto out_free;
306                         }
307                 }
308                 mutex_lock(&dev->struct_mutex);
309                 if (dev->driver->master_set) {
310                         ret = dev->driver->master_set(dev, priv, true);
311                         if (ret) {
312                                 /* drop both references if this fails */
313                                 drm_master_put(&priv->minor->master);
314                                 drm_master_put(&priv->master);
315                                 mutex_unlock(&dev->struct_mutex);
316                                 goto out_free;
317                         }
318                 }
319                 mutex_unlock(&dev->struct_mutex);
320         } else {
321                 /* get a reference to the master */
322                 priv->master = drm_master_get(priv->minor->master);
323                 mutex_unlock(&dev->struct_mutex);
324         }
325
326         mutex_lock(&dev->struct_mutex);
327         list_add(&priv->lhead, &dev->filelist);
328         mutex_unlock(&dev->struct_mutex);
329
330 #ifdef __alpha__
331         /*
332          * Default the hose
333          */
334         if (!dev->hose) {
335                 struct pci_dev *pci_dev;
336                 pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
337                 if (pci_dev) {
338                         dev->hose = pci_dev->sysdata;
339                         pci_dev_put(pci_dev);
340                 }
341                 if (!dev->hose) {
342                         struct pci_bus *b = pci_bus_b(pci_root_buses.next);
343                         if (b)
344                                 dev->hose = b->sysdata;
345                 }
346         }
347 #endif
348
349         return 0;
350       out_free:
351         kfree(priv);
352         filp->private_data = NULL;
353         return ret;
354 }
355
356 /** No-op. */
357 int drm_fasync(int fd, struct file *filp, int on)
358 {
359         struct drm_file *priv = filp->private_data;
360         struct drm_device *dev = priv->minor->dev;
361
362         DRM_DEBUG("fd = %d, device = 0x%lx\n", fd,
363                   (long)old_encode_dev(priv->minor->device));
364         return fasync_helper(fd, filp, on, &dev->buf_async);
365 }
366 EXPORT_SYMBOL(drm_fasync);
367
368 /*
369  * Reclaim locked buffers; note that this may be a bad idea if the current
370  * context doesn't have the hw lock...
371  */
372 static void drm_reclaim_locked_buffers(struct drm_device *dev, struct file *f)
373 {
374         struct drm_file *file_priv = f->private_data;
375
376         if (drm_i_have_hw_lock(dev, file_priv)) {
377                 dev->driver->reclaim_buffers_locked(dev, file_priv);
378         } else {
379                 unsigned long _end = jiffies + 3 * DRM_HZ;
380                 int locked = 0;
381
382                 drm_idlelock_take(&file_priv->master->lock);
383
384                 /*
385                  * Wait for a while.
386                  */
387                 do {
388                         spin_lock_bh(&file_priv->master->lock.spinlock);
389                         locked = file_priv->master->lock.idle_has_lock;
390                         spin_unlock_bh(&file_priv->master->lock.spinlock);
391                         if (locked)
392                                 break;
393                         schedule();
394                 } while (!time_after_eq(jiffies, _end));
395
396                 if (!locked) {
397                         DRM_ERROR("reclaim_buffers_locked() deadlock. Please rework this\n"
398                                   "\tdriver to use reclaim_buffers_idlelocked() instead.\n"
399                                   "\tI will go on reclaiming the buffers anyway.\n");
400                 }
401
402                 dev->driver->reclaim_buffers_locked(dev, file_priv);
403                 drm_idlelock_release(&file_priv->master->lock);
404         }
405 }
406
407 static void drm_master_release(struct drm_device *dev, struct file *filp)
408 {
409         struct drm_file *file_priv = filp->private_data;
410
411         if (dev->driver->reclaim_buffers_locked &&
412             file_priv->master->lock.hw_lock)
413                 drm_reclaim_locked_buffers(dev, filp);
414
415         if (dev->driver->reclaim_buffers_idlelocked &&
416             file_priv->master->lock.hw_lock) {
417                 drm_idlelock_take(&file_priv->master->lock);
418                 dev->driver->reclaim_buffers_idlelocked(dev, file_priv);
419                 drm_idlelock_release(&file_priv->master->lock);
420         }
421
422
423         if (drm_i_have_hw_lock(dev, file_priv)) {
424                 DRM_DEBUG("File %p released, freeing lock for context %d\n",
425                           filp, _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
426                 drm_lock_free(&file_priv->master->lock,
427                               _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
428         }
429
430         if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
431             !dev->driver->reclaim_buffers_locked) {
432                 dev->driver->reclaim_buffers(dev, file_priv);
433         }
434 }
435
436 static void drm_events_release(struct drm_file *file_priv)
437 {
438         struct drm_device *dev = file_priv->minor->dev;
439         struct drm_pending_event *e, *et;
440         struct drm_pending_vblank_event *v, *vt;
441         unsigned long flags;
442
443         spin_lock_irqsave(&dev->event_lock, flags);
444
445         /* Remove pending flips */
446         list_for_each_entry_safe(v, vt, &dev->vblank_event_list, base.link)
447                 if (v->base.file_priv == file_priv) {
448                         list_del(&v->base.link);
449                         drm_vblank_put(dev, v->pipe);
450                         v->base.destroy(&v->base);
451                 }
452
453         /* Remove unconsumed events */
454         list_for_each_entry_safe(e, et, &file_priv->event_list, link)
455                 e->destroy(e);
456
457         spin_unlock_irqrestore(&dev->event_lock, flags);
458 }
459
460 /**
461  * Release file.
462  *
463  * \param inode device inode
464  * \param file_priv DRM file private.
465  * \return zero on success or a negative number on failure.
466  *
467  * If the hardware lock is held then free it, and take it again for the kernel
468  * context since it's necessary to reclaim buffers. Unlink the file private
469  * data from its list and free it. Decreases the open count and if it reaches
470  * zero calls drm_lastclose().
471  */
472 int drm_release(struct inode *inode, struct file *filp)
473 {
474         struct drm_file *file_priv = filp->private_data;
475         struct drm_device *dev = file_priv->minor->dev;
476         int retcode = 0;
477
478         mutex_lock(&drm_global_mutex);
479
480         DRM_DEBUG("open_count = %d\n", dev->open_count);
481
482         if (dev->driver->preclose)
483                 dev->driver->preclose(dev, file_priv);
484
485         /* ========================================================
486          * Begin inline drm_release
487          */
488
489         DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
490                   task_pid_nr(current),
491                   (long)old_encode_dev(file_priv->minor->device),
492                   dev->open_count);
493
494         /* if the master has gone away we can't do anything with the lock */
495         if (file_priv->minor->master)
496                 drm_master_release(dev, filp);
497
498         drm_events_release(file_priv);
499
500         if (dev->driver->driver_features & DRIVER_GEM)
501                 drm_gem_release(dev, file_priv);
502
503         if (dev->driver->driver_features & DRIVER_MODESET)
504                 drm_fb_release(file_priv);
505
506         mutex_lock(&dev->ctxlist_mutex);
507         if (!list_empty(&dev->ctxlist)) {
508                 struct drm_ctx_list *pos, *n;
509
510                 list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
511                         if (pos->tag == file_priv &&
512                             pos->handle != DRM_KERNEL_CONTEXT) {
513                                 if (dev->driver->context_dtor)
514                                         dev->driver->context_dtor(dev,
515                                                                   pos->handle);
516
517                                 drm_ctxbitmap_free(dev, pos->handle);
518
519                                 list_del(&pos->head);
520                                 kfree(pos);
521                                 --dev->ctx_count;
522                         }
523                 }
524         }
525         mutex_unlock(&dev->ctxlist_mutex);
526
527         mutex_lock(&dev->struct_mutex);
528
529         if (file_priv->is_master) {
530                 struct drm_master *master = file_priv->master;
531                 struct drm_file *temp;
532                 list_for_each_entry(temp, &dev->filelist, lhead) {
533                         if ((temp->master == file_priv->master) &&
534                             (temp != file_priv))
535                                 temp->authenticated = 0;
536                 }
537
538                 /**
539                  * Since the master is disappearing, so is the
540                  * possibility to lock.
541                  */
542
543                 if (master->lock.hw_lock) {
544                         if (dev->sigdata.lock == master->lock.hw_lock)
545                                 dev->sigdata.lock = NULL;
546                         master->lock.hw_lock = NULL;
547                         master->lock.file_priv = NULL;
548                         wake_up_interruptible_all(&master->lock.lock_queue);
549                 }
550
551                 if (file_priv->minor->master == file_priv->master) {
552                         /* drop the reference held my the minor */
553                         if (dev->driver->master_drop)
554                                 dev->driver->master_drop(dev, file_priv, true);
555                         drm_master_put(&file_priv->minor->master);
556                 }
557         }
558
559         /* drop the reference held my the file priv */
560         drm_master_put(&file_priv->master);
561         file_priv->is_master = 0;
562         list_del(&file_priv->lhead);
563         mutex_unlock(&dev->struct_mutex);
564
565         if (dev->driver->postclose)
566                 dev->driver->postclose(dev, file_priv);
567         kfree(file_priv);
568
569         /* ========================================================
570          * End inline drm_release
571          */
572
573         atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
574         spin_lock(&dev->count_lock);
575         if (!--dev->open_count) {
576                 if (atomic_read(&dev->ioctl_count)) {
577                         DRM_ERROR("Device busy: %d\n",
578                                   atomic_read(&dev->ioctl_count));
579                         retcode = -EBUSY;
580                         goto out;
581                 }
582                 retcode = drm_lastclose(dev);
583         }
584 out:
585         spin_unlock(&dev->count_lock);
586         mutex_unlock(&drm_global_mutex);
587
588         return retcode;
589 }
590 EXPORT_SYMBOL(drm_release);
591
592 static bool
593 drm_dequeue_event(struct drm_file *file_priv,
594                   size_t total, size_t max, struct drm_pending_event **out)
595 {
596         struct drm_device *dev = file_priv->minor->dev;
597         struct drm_pending_event *e;
598         unsigned long flags;
599         bool ret = false;
600
601         spin_lock_irqsave(&dev->event_lock, flags);
602
603         *out = NULL;
604         if (list_empty(&file_priv->event_list))
605                 goto out;
606         e = list_first_entry(&file_priv->event_list,
607                              struct drm_pending_event, link);
608         if (e->event->length + total > max)
609                 goto out;
610
611         file_priv->event_space += e->event->length;
612         list_del(&e->link);
613         *out = e;
614         ret = true;
615
616 out:
617         spin_unlock_irqrestore(&dev->event_lock, flags);
618         return ret;
619 }
620
621 ssize_t drm_read(struct file *filp, char __user *buffer,
622                  size_t count, loff_t *offset)
623 {
624         struct drm_file *file_priv = filp->private_data;
625         struct drm_pending_event *e;
626         size_t total;
627         ssize_t ret;
628
629         ret = wait_event_interruptible(file_priv->event_wait,
630                                        !list_empty(&file_priv->event_list));
631         if (ret < 0)
632                 return ret;
633
634         total = 0;
635         while (drm_dequeue_event(file_priv, total, count, &e)) {
636                 if (copy_to_user(buffer + total,
637                                  e->event, e->event->length)) {
638                         total = -EFAULT;
639                         break;
640                 }
641
642                 total += e->event->length;
643                 e->destroy(e);
644         }
645
646         return total;
647 }
648 EXPORT_SYMBOL(drm_read);
649
650 unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
651 {
652         struct drm_file *file_priv = filp->private_data;
653         unsigned int mask = 0;
654
655         poll_wait(filp, &file_priv->event_wait, wait);
656
657         if (!list_empty(&file_priv->event_list))
658                 mask |= POLLIN | POLLRDNORM;
659
660         return mask;
661 }
662 EXPORT_SYMBOL(drm_poll);