drm: Reject page_flip for !DRIVER_MODESET
[pandora-kernel.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/module.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                 if (!dev->open_count++) {
140                         retcode = drm_setup(dev);
141                         if (retcode)
142                                 dev->open_count--;
143                 }
144         }
145         if (!retcode) {
146                 mutex_lock(&dev->struct_mutex);
147                 if (minor->type == DRM_MINOR_LEGACY) {
148                         if (dev->dev_mapping == NULL)
149                                 dev->dev_mapping = inode->i_mapping;
150                         else if (dev->dev_mapping != inode->i_mapping)
151                                 retcode = -ENODEV;
152                 }
153                 mutex_unlock(&dev->struct_mutex);
154         }
155
156         return retcode;
157 }
158 EXPORT_SYMBOL(drm_open);
159
160 /**
161  * File \c open operation.
162  *
163  * \param inode device inode.
164  * \param filp file pointer.
165  *
166  * Puts the dev->fops corresponding to the device minor number into
167  * \p filp, call the \c open method, and restore the file operations.
168  */
169 int drm_stub_open(struct inode *inode, struct file *filp)
170 {
171         struct drm_device *dev = NULL;
172         struct drm_minor *minor;
173         int minor_id = iminor(inode);
174         int err = -ENODEV;
175         const struct file_operations *old_fops;
176
177         DRM_DEBUG("\n");
178
179         mutex_lock(&drm_global_mutex);
180         minor = idr_find(&drm_minors_idr, minor_id);
181         if (!minor)
182                 goto out;
183
184         if (!(dev = minor->dev))
185                 goto out;
186
187         old_fops = filp->f_op;
188         filp->f_op = fops_get(&dev->driver->fops);
189         if (filp->f_op == NULL) {
190                 filp->f_op = old_fops;
191                 goto out;
192         }
193         if (filp->f_op->open && (err = filp->f_op->open(inode, filp))) {
194                 fops_put(filp->f_op);
195                 filp->f_op = fops_get(old_fops);
196         }
197         fops_put(old_fops);
198
199 out:
200         mutex_unlock(&drm_global_mutex);
201         return err;
202 }
203
204 /**
205  * Check whether DRI will run on this CPU.
206  *
207  * \return non-zero if the DRI will run on this CPU, or zero otherwise.
208  */
209 static int drm_cpu_valid(void)
210 {
211 #if defined(__i386__)
212         if (boot_cpu_data.x86 == 3)
213                 return 0;       /* No cmpxchg on a 386 */
214 #endif
215 #if defined(__sparc__) && !defined(__sparc_v9__)
216         return 0;               /* No cmpxchg before v9 sparc. */
217 #endif
218         return 1;
219 }
220
221 /**
222  * drm_new_set_master - Allocate a new master object and become master for the
223  * associated master realm.
224  *
225  * @dev: The associated device.
226  * @fpriv: File private identifying the client.
227  *
228  * This function must be called with dev::struct_mutex held.
229  * Returns negative error code on failure. Zero on success.
230  */
231 int drm_new_set_master(struct drm_device *dev, struct drm_file *fpriv)
232 {
233         struct drm_master *old_master;
234         int ret;
235
236         lockdep_assert_held_once(&dev->struct_mutex);
237
238         /* create a new master */
239         fpriv->minor->master = drm_master_create(fpriv->minor);
240         if (!fpriv->minor->master)
241                 return -ENOMEM;
242
243         /* take another reference for the copy in the local file priv */
244         old_master = fpriv->master;
245         fpriv->master = drm_master_get(fpriv->minor->master);
246
247         if (dev->driver->master_create) {
248                 mutex_unlock(&dev->struct_mutex);
249                 ret = dev->driver->master_create(dev, fpriv->master);
250                 mutex_lock(&dev->struct_mutex);
251                 if (ret)
252                         goto out_err;
253         }
254         if (dev->driver->master_set) {
255                 ret = dev->driver->master_set(dev, fpriv, true);
256                 if (ret)
257                         goto out_err;
258         }
259
260         fpriv->is_master = 1;
261         fpriv->allowed_master = 1;
262         fpriv->authenticated = 1;
263         if (old_master)
264                 drm_master_put(&old_master);
265
266         return 0;
267
268 out_err:
269         /* drop both references and restore old master on failure */
270         drm_master_put(&fpriv->minor->master);
271         drm_master_put(&fpriv->master);
272         fpriv->master = old_master;
273
274         return ret;
275 }
276
277 /**
278  * Called whenever a process opens /dev/drm.
279  *
280  * \param inode device inode.
281  * \param filp file pointer.
282  * \param dev device.
283  * \return zero on success or a negative number on failure.
284  *
285  * Creates and initializes a drm_file structure for the file private data in \p
286  * filp and add it into the double linked list in \p dev.
287  */
288 static int drm_open_helper(struct inode *inode, struct file *filp,
289                            struct drm_device * dev)
290 {
291         int minor_id = iminor(inode);
292         struct drm_file *priv;
293         int ret;
294
295         if (filp->f_flags & O_EXCL)
296                 return -EBUSY;  /* No exclusive opens */
297         if (!drm_cpu_valid())
298                 return -EINVAL;
299         if (dev->switch_power_state != DRM_SWITCH_POWER_ON)
300                 return -EINVAL;
301
302         DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current), minor_id);
303
304         priv = kzalloc(sizeof(*priv), GFP_KERNEL);
305         if (!priv)
306                 return -ENOMEM;
307
308         filp->private_data = priv;
309         priv->filp = filp;
310         priv->uid = current_euid();
311         priv->pid = task_pid_nr(current);
312         priv->minor = idr_find(&drm_minors_idr, minor_id);
313         priv->ioctl_count = 0;
314         /* for compatibility root is always authenticated */
315         priv->authenticated = capable(CAP_SYS_ADMIN);
316         priv->lock_count = 0;
317
318         INIT_LIST_HEAD(&priv->lhead);
319         INIT_LIST_HEAD(&priv->fbs);
320         INIT_LIST_HEAD(&priv->event_list);
321         init_waitqueue_head(&priv->event_wait);
322         priv->event_space = 4096; /* set aside 4k for event buffer */
323
324         if (dev->driver->driver_features & DRIVER_GEM)
325                 drm_gem_open(dev, priv);
326
327         if (dev->driver->open) {
328                 ret = dev->driver->open(dev, priv);
329                 if (ret < 0)
330                         goto out_free;
331         }
332
333
334         /* if there is no current master make this fd it */
335         mutex_lock(&dev->struct_mutex);
336         if (!priv->minor->master) {
337                 /* create a new master */
338                 ret = drm_new_set_master(dev, priv);
339                 mutex_unlock(&dev->struct_mutex);
340                 if (ret)
341                         goto out_free;
342         } else {
343                 /* get a reference to the master */
344                 priv->master = drm_master_get(priv->minor->master);
345                 mutex_unlock(&dev->struct_mutex);
346         }
347
348         mutex_lock(&dev->struct_mutex);
349         list_add(&priv->lhead, &dev->filelist);
350         mutex_unlock(&dev->struct_mutex);
351
352 #ifdef __alpha__
353         /*
354          * Default the hose
355          */
356         if (!dev->hose) {
357                 struct pci_dev *pci_dev;
358                 pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
359                 if (pci_dev) {
360                         dev->hose = pci_dev->sysdata;
361                         pci_dev_put(pci_dev);
362                 }
363                 if (!dev->hose) {
364                         struct pci_bus *b = pci_bus_b(pci_root_buses.next);
365                         if (b)
366                                 dev->hose = b->sysdata;
367                 }
368         }
369 #endif
370
371         return 0;
372       out_free:
373         kfree(priv);
374         filp->private_data = NULL;
375         return ret;
376 }
377
378 /** No-op. */
379 int drm_fasync(int fd, struct file *filp, int on)
380 {
381         struct drm_file *priv = filp->private_data;
382         struct drm_device *dev = priv->minor->dev;
383
384         DRM_DEBUG("fd = %d, device = 0x%lx\n", fd,
385                   (long)old_encode_dev(priv->minor->device));
386         return fasync_helper(fd, filp, on, &dev->buf_async);
387 }
388 EXPORT_SYMBOL(drm_fasync);
389
390 /*
391  * Reclaim locked buffers; note that this may be a bad idea if the current
392  * context doesn't have the hw lock...
393  */
394 static void drm_reclaim_locked_buffers(struct drm_device *dev, struct file *f)
395 {
396         struct drm_file *file_priv = f->private_data;
397
398         if (drm_i_have_hw_lock(dev, file_priv)) {
399                 dev->driver->reclaim_buffers_locked(dev, file_priv);
400         } else {
401                 unsigned long _end = jiffies + 3 * DRM_HZ;
402                 int locked = 0;
403
404                 drm_idlelock_take(&file_priv->master->lock);
405
406                 /*
407                  * Wait for a while.
408                  */
409                 do {
410                         spin_lock_bh(&file_priv->master->lock.spinlock);
411                         locked = file_priv->master->lock.idle_has_lock;
412                         spin_unlock_bh(&file_priv->master->lock.spinlock);
413                         if (locked)
414                                 break;
415                         schedule();
416                 } while (!time_after_eq(jiffies, _end));
417
418                 if (!locked) {
419                         DRM_ERROR("reclaim_buffers_locked() deadlock. Please rework this\n"
420                                   "\tdriver to use reclaim_buffers_idlelocked() instead.\n"
421                                   "\tI will go on reclaiming the buffers anyway.\n");
422                 }
423
424                 dev->driver->reclaim_buffers_locked(dev, file_priv);
425                 drm_idlelock_release(&file_priv->master->lock);
426         }
427 }
428
429 static void drm_master_release(struct drm_device *dev, struct file *filp)
430 {
431         struct drm_file *file_priv = filp->private_data;
432
433         if (dev->driver->reclaim_buffers_locked &&
434             file_priv->master->lock.hw_lock)
435                 drm_reclaim_locked_buffers(dev, filp);
436
437         if (dev->driver->reclaim_buffers_idlelocked &&
438             file_priv->master->lock.hw_lock) {
439                 drm_idlelock_take(&file_priv->master->lock);
440                 dev->driver->reclaim_buffers_idlelocked(dev, file_priv);
441                 drm_idlelock_release(&file_priv->master->lock);
442         }
443
444
445         if (drm_i_have_hw_lock(dev, file_priv)) {
446                 DRM_DEBUG("File %p released, freeing lock for context %d\n",
447                           filp, _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
448                 drm_lock_free(&file_priv->master->lock,
449                               _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
450         }
451
452         if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
453             !dev->driver->reclaim_buffers_locked) {
454                 dev->driver->reclaim_buffers(dev, file_priv);
455         }
456 }
457
458 static void drm_events_release(struct drm_file *file_priv)
459 {
460         struct drm_device *dev = file_priv->minor->dev;
461         struct drm_pending_event *e, *et;
462         struct drm_pending_vblank_event *v, *vt;
463         unsigned long flags;
464
465         spin_lock_irqsave(&dev->event_lock, flags);
466
467         /* Remove pending flips */
468         list_for_each_entry_safe(v, vt, &dev->vblank_event_list, base.link)
469                 if (v->base.file_priv == file_priv) {
470                         list_del(&v->base.link);
471                         drm_vblank_put(dev, v->pipe);
472                         v->base.destroy(&v->base);
473                 }
474
475         /* Remove unconsumed events */
476         list_for_each_entry_safe(e, et, &file_priv->event_list, link)
477                 e->destroy(e);
478
479         spin_unlock_irqrestore(&dev->event_lock, flags);
480 }
481
482 /**
483  * Release file.
484  *
485  * \param inode device inode
486  * \param file_priv DRM file private.
487  * \return zero on success or a negative number on failure.
488  *
489  * If the hardware lock is held then free it, and take it again for the kernel
490  * context since it's necessary to reclaim buffers. Unlink the file private
491  * data from its list and free it. Decreases the open count and if it reaches
492  * zero calls drm_lastclose().
493  */
494 int drm_release(struct inode *inode, struct file *filp)
495 {
496         struct drm_file *file_priv = filp->private_data;
497         struct drm_device *dev = file_priv->minor->dev;
498         int retcode = 0;
499
500         mutex_lock(&drm_global_mutex);
501
502         DRM_DEBUG("open_count = %d\n", dev->open_count);
503
504         if (dev->driver->preclose)
505                 dev->driver->preclose(dev, file_priv);
506
507         /* ========================================================
508          * Begin inline drm_release
509          */
510
511         DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
512                   task_pid_nr(current),
513                   (long)old_encode_dev(file_priv->minor->device),
514                   dev->open_count);
515
516         /* Release any auth tokens that might point to this file_priv,
517            (do that under the drm_global_mutex) */
518         if (file_priv->magic)
519                 (void) drm_remove_magic(file_priv->master, file_priv->magic);
520
521         /* if the master has gone away we can't do anything with the lock */
522         if (file_priv->minor->master)
523                 drm_master_release(dev, filp);
524
525         drm_events_release(file_priv);
526
527         if (dev->driver->driver_features & DRIVER_GEM)
528                 drm_gem_release(dev, file_priv);
529
530         if (dev->driver->driver_features & DRIVER_MODESET)
531                 drm_fb_release(file_priv);
532
533         mutex_lock(&dev->ctxlist_mutex);
534         if (!list_empty(&dev->ctxlist)) {
535                 struct drm_ctx_list *pos, *n;
536
537                 list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
538                         if (pos->tag == file_priv &&
539                             pos->handle != DRM_KERNEL_CONTEXT) {
540                                 if (dev->driver->context_dtor)
541                                         dev->driver->context_dtor(dev,
542                                                                   pos->handle);
543
544                                 drm_ctxbitmap_free(dev, pos->handle);
545
546                                 list_del(&pos->head);
547                                 kfree(pos);
548                                 --dev->ctx_count;
549                         }
550                 }
551         }
552         mutex_unlock(&dev->ctxlist_mutex);
553
554         mutex_lock(&dev->struct_mutex);
555
556         if (file_priv->is_master) {
557                 struct drm_master *master = file_priv->master;
558                 struct drm_file *temp;
559                 list_for_each_entry(temp, &dev->filelist, lhead) {
560                         if ((temp->master == file_priv->master) &&
561                             (temp != file_priv))
562                                 temp->authenticated = 0;
563                 }
564
565                 /**
566                  * Since the master is disappearing, so is the
567                  * possibility to lock.
568                  */
569
570                 if (master->lock.hw_lock) {
571                         if (dev->sigdata.lock == master->lock.hw_lock)
572                                 dev->sigdata.lock = NULL;
573                         master->lock.hw_lock = NULL;
574                         master->lock.file_priv = NULL;
575                         wake_up_interruptible_all(&master->lock.lock_queue);
576                 }
577
578                 if (file_priv->minor->master == file_priv->master) {
579                         /* drop the reference held my the minor */
580                         if (dev->driver->master_drop)
581                                 dev->driver->master_drop(dev, file_priv, true);
582                         drm_master_put(&file_priv->minor->master);
583                 }
584         }
585
586         /* drop the reference held my the file priv */
587         drm_master_put(&file_priv->master);
588         file_priv->is_master = 0;
589         list_del(&file_priv->lhead);
590         mutex_unlock(&dev->struct_mutex);
591
592         if (dev->driver->postclose)
593                 dev->driver->postclose(dev, file_priv);
594         kfree(file_priv);
595
596         /* ========================================================
597          * End inline drm_release
598          */
599
600         atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
601         if (!--dev->open_count) {
602                 if (atomic_read(&dev->ioctl_count)) {
603                         DRM_ERROR("Device busy: %d\n",
604                                   atomic_read(&dev->ioctl_count));
605                         retcode = -EBUSY;
606                 } else
607                         retcode = drm_lastclose(dev);
608         }
609         mutex_unlock(&drm_global_mutex);
610
611         return retcode;
612 }
613 EXPORT_SYMBOL(drm_release);
614
615 static bool
616 drm_dequeue_event(struct drm_file *file_priv,
617                   size_t total, size_t max, struct drm_pending_event **out)
618 {
619         struct drm_device *dev = file_priv->minor->dev;
620         struct drm_pending_event *e;
621         unsigned long flags;
622         bool ret = false;
623
624         spin_lock_irqsave(&dev->event_lock, flags);
625
626         *out = NULL;
627         if (list_empty(&file_priv->event_list))
628                 goto out;
629         e = list_first_entry(&file_priv->event_list,
630                              struct drm_pending_event, link);
631         if (e->event->length + total > max)
632                 goto out;
633
634         file_priv->event_space += e->event->length;
635         list_del(&e->link);
636         *out = e;
637         ret = true;
638
639 out:
640         spin_unlock_irqrestore(&dev->event_lock, flags);
641         return ret;
642 }
643
644 ssize_t drm_read(struct file *filp, char __user *buffer,
645                  size_t count, loff_t *offset)
646 {
647         struct drm_file *file_priv = filp->private_data;
648         struct drm_pending_event *e;
649         size_t total;
650         ssize_t ret;
651
652         ret = wait_event_interruptible(file_priv->event_wait,
653                                        !list_empty(&file_priv->event_list));
654         if (ret < 0)
655                 return ret;
656
657         total = 0;
658         while (drm_dequeue_event(file_priv, total, count, &e)) {
659                 if (copy_to_user(buffer + total,
660                                  e->event, e->event->length)) {
661                         total = -EFAULT;
662                         break;
663                 }
664
665                 total += e->event->length;
666                 e->destroy(e);
667         }
668
669         return total;
670 }
671 EXPORT_SYMBOL(drm_read);
672
673 unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
674 {
675         struct drm_file *file_priv = filp->private_data;
676         unsigned int mask = 0;
677
678         poll_wait(filp, &file_priv->event_wait, wait);
679
680         if (!list_empty(&file_priv->event_list))
681                 mask |= POLLIN | POLLRDNORM;
682
683         return mask;
684 }
685 EXPORT_SYMBOL(drm_poll);