c14fdc1c109bcdbddb3c3cd6fd3adc33da6404f4
[linux-2.6-microblaze.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 <drm/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->context_flag = 0;
79         dev->interrupt_flag = 0;
80         dev->dma_flag = 0;
81         dev->last_context = 0;
82         dev->last_checked = 0;
83         dev->if_version = 0;
84
85         dev->ctx_start = 0;
86         dev->lck_start = 0;
87
88         dev->buf_async = NULL;
89         init_waitqueue_head(&dev->buf_readers);
90         init_waitqueue_head(&dev->buf_writers);
91
92         DRM_DEBUG("\n");
93
94         /*
95          * The kernel's context could be created here, but is now created
96          * in drm_dma_enqueue.  This is more resource-efficient for
97          * hardware that does not do DMA, but may mean that
98          * drm_select_queue fails between the time the interrupt is
99          * initialized and the time the queues are initialized.
100          */
101
102         return 0;
103 }
104
105 /**
106  * Open file.
107  *
108  * \param inode device inode
109  * \param filp file pointer.
110  * \return zero on success or a negative number on failure.
111  *
112  * Searches the DRM device with the same minor number, calls open_helper(), and
113  * increments the device open count. If the open count was previous at zero,
114  * i.e., it's the first that the device is open, then calls setup().
115  */
116 int drm_open(struct inode *inode, struct file *filp)
117 {
118         struct drm_device *dev = NULL;
119         int minor_id = iminor(inode);
120         struct drm_minor *minor;
121         int retcode = 0;
122         int need_setup = 0;
123         struct address_space *old_mapping;
124         struct address_space *old_imapping;
125
126         minor = idr_find(&drm_minors_idr, minor_id);
127         if (!minor)
128                 return -ENODEV;
129
130         if (!(dev = minor->dev))
131                 return -ENODEV;
132
133         if (drm_device_is_unplugged(dev))
134                 return -ENODEV;
135
136         if (!dev->open_count++)
137                 need_setup = 1;
138         mutex_lock(&dev->struct_mutex);
139         old_imapping = inode->i_mapping;
140         old_mapping = dev->dev_mapping;
141         if (old_mapping == NULL)
142                 dev->dev_mapping = &inode->i_data;
143         /* ihold ensures nobody can remove inode with our i_data */
144         ihold(container_of(dev->dev_mapping, struct inode, i_data));
145         inode->i_mapping = dev->dev_mapping;
146         filp->f_mapping = dev->dev_mapping;
147         mutex_unlock(&dev->struct_mutex);
148
149         retcode = drm_open_helper(inode, filp, dev);
150         if (retcode)
151                 goto err_undo;
152         atomic_inc(&dev->counts[_DRM_STAT_OPENS]);
153         if (need_setup) {
154                 retcode = drm_setup(dev);
155                 if (retcode)
156                         goto err_undo;
157         }
158         return 0;
159
160 err_undo:
161         mutex_lock(&dev->struct_mutex);
162         filp->f_mapping = old_imapping;
163         inode->i_mapping = old_imapping;
164         iput(container_of(dev->dev_mapping, struct inode, i_data));
165         dev->dev_mapping = old_mapping;
166         mutex_unlock(&dev->struct_mutex);
167         dev->open_count--;
168         return retcode;
169 }
170 EXPORT_SYMBOL(drm_open);
171
172 /**
173  * File \c open operation.
174  *
175  * \param inode device inode.
176  * \param filp file pointer.
177  *
178  * Puts the dev->fops corresponding to the device minor number into
179  * \p filp, call the \c open method, and restore the file operations.
180  */
181 int drm_stub_open(struct inode *inode, struct file *filp)
182 {
183         struct drm_device *dev = NULL;
184         struct drm_minor *minor;
185         int minor_id = iminor(inode);
186         int err = -ENODEV;
187         const struct file_operations *old_fops;
188
189         DRM_DEBUG("\n");
190
191         mutex_lock(&drm_global_mutex);
192         minor = idr_find(&drm_minors_idr, minor_id);
193         if (!minor)
194                 goto out;
195
196         if (!(dev = minor->dev))
197                 goto out;
198
199         if (drm_device_is_unplugged(dev))
200                 goto out;
201
202         old_fops = filp->f_op;
203         filp->f_op = fops_get(dev->driver->fops);
204         if (filp->f_op == NULL) {
205                 filp->f_op = old_fops;
206                 goto out;
207         }
208         if (filp->f_op->open && (err = filp->f_op->open(inode, filp))) {
209                 fops_put(filp->f_op);
210                 filp->f_op = fops_get(old_fops);
211         }
212         fops_put(old_fops);
213
214 out:
215         mutex_unlock(&drm_global_mutex);
216         return err;
217 }
218
219 /**
220  * Check whether DRI will run on this CPU.
221  *
222  * \return non-zero if the DRI will run on this CPU, or zero otherwise.
223  */
224 static int drm_cpu_valid(void)
225 {
226 #if defined(__i386__)
227         if (boot_cpu_data.x86 == 3)
228                 return 0;       /* No cmpxchg on a 386 */
229 #endif
230 #if defined(__sparc__) && !defined(__sparc_v9__)
231         return 0;               /* No cmpxchg before v9 sparc. */
232 #endif
233         return 1;
234 }
235
236 /**
237  * Called whenever a process opens /dev/drm.
238  *
239  * \param inode device inode.
240  * \param filp file pointer.
241  * \param dev device.
242  * \return zero on success or a negative number on failure.
243  *
244  * Creates and initializes a drm_file structure for the file private data in \p
245  * filp and add it into the double linked list in \p dev.
246  */
247 static int drm_open_helper(struct inode *inode, struct file *filp,
248                            struct drm_device * dev)
249 {
250         int minor_id = iminor(inode);
251         struct drm_file *priv;
252         int ret;
253
254         if (filp->f_flags & O_EXCL)
255                 return -EBUSY;  /* No exclusive opens */
256         if (!drm_cpu_valid())
257                 return -EINVAL;
258         if (dev->switch_power_state != DRM_SWITCH_POWER_ON)
259                 return -EINVAL;
260
261         DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current), minor_id);
262
263         priv = kzalloc(sizeof(*priv), GFP_KERNEL);
264         if (!priv)
265                 return -ENOMEM;
266
267         filp->private_data = priv;
268         priv->filp = filp;
269         priv->uid = current_euid();
270         priv->pid = get_pid(task_pid(current));
271         priv->minor = idr_find(&drm_minors_idr, minor_id);
272         if (!priv->minor) {
273                 ret = -ENODEV;
274                 goto out_put_pid;
275         }
276
277         priv->ioctl_count = 0;
278         /* for compatibility root is always authenticated */
279         priv->authenticated = capable(CAP_SYS_ADMIN);
280         priv->lock_count = 0;
281
282         INIT_LIST_HEAD(&priv->lhead);
283         INIT_LIST_HEAD(&priv->fbs);
284         mutex_init(&priv->fbs_lock);
285         INIT_LIST_HEAD(&priv->event_list);
286         init_waitqueue_head(&priv->event_wait);
287         priv->event_space = 4096; /* set aside 4k for event buffer */
288
289         if (dev->driver->driver_features & DRIVER_GEM)
290                 drm_gem_open(dev, priv);
291
292         if (drm_core_check_feature(dev, DRIVER_PRIME))
293                 drm_prime_init_file_private(&priv->prime);
294
295         if (dev->driver->open) {
296                 ret = dev->driver->open(dev, priv);
297                 if (ret < 0)
298                         goto out_prime_destroy;
299         }
300
301
302         /* if there is no current master make this fd it */
303         mutex_lock(&dev->struct_mutex);
304         if (!priv->minor->master) {
305                 /* create a new master */
306                 priv->minor->master = drm_master_create(priv->minor);
307                 if (!priv->minor->master) {
308                         mutex_unlock(&dev->struct_mutex);
309                         ret = -ENOMEM;
310                         goto out_close;
311                 }
312
313                 priv->is_master = 1;
314                 /* take another reference for the copy in the local file priv */
315                 priv->master = drm_master_get(priv->minor->master);
316
317                 priv->authenticated = 1;
318
319                 mutex_unlock(&dev->struct_mutex);
320                 if (dev->driver->master_create) {
321                         ret = dev->driver->master_create(dev, priv->master);
322                         if (ret) {
323                                 mutex_lock(&dev->struct_mutex);
324                                 /* drop both references if this fails */
325                                 drm_master_put(&priv->minor->master);
326                                 drm_master_put(&priv->master);
327                                 mutex_unlock(&dev->struct_mutex);
328                                 goto out_close;
329                         }
330                 }
331                 mutex_lock(&dev->struct_mutex);
332                 if (dev->driver->master_set) {
333                         ret = dev->driver->master_set(dev, priv, true);
334                         if (ret) {
335                                 /* drop both references if this fails */
336                                 drm_master_put(&priv->minor->master);
337                                 drm_master_put(&priv->master);
338                                 mutex_unlock(&dev->struct_mutex);
339                                 goto out_close;
340                         }
341                 }
342                 mutex_unlock(&dev->struct_mutex);
343         } else {
344                 /* get a reference to the master */
345                 priv->master = drm_master_get(priv->minor->master);
346                 mutex_unlock(&dev->struct_mutex);
347         }
348
349         mutex_lock(&dev->struct_mutex);
350         list_add(&priv->lhead, &dev->filelist);
351         mutex_unlock(&dev->struct_mutex);
352
353 #ifdef __alpha__
354         /*
355          * Default the hose
356          */
357         if (!dev->hose) {
358                 struct pci_dev *pci_dev;
359                 pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
360                 if (pci_dev) {
361                         dev->hose = pci_dev->sysdata;
362                         pci_dev_put(pci_dev);
363                 }
364                 if (!dev->hose) {
365                         struct pci_bus *b = pci_bus_b(pci_root_buses.next);
366                         if (b)
367                                 dev->hose = b->sysdata;
368                 }
369         }
370 #endif
371
372         return 0;
373
374 out_close:
375         if (dev->driver->postclose)
376                 dev->driver->postclose(dev, priv);
377 out_prime_destroy:
378         if (drm_core_check_feature(dev, DRIVER_PRIME))
379                 drm_prime_destroy_file_private(&priv->prime);
380         if (dev->driver->driver_features & DRIVER_GEM)
381                 drm_gem_release(dev, priv);
382 out_put_pid:
383         put_pid(priv->pid);
384         kfree(priv);
385         filp->private_data = NULL;
386         return ret;
387 }
388
389 /** No-op. */
390 int drm_fasync(int fd, struct file *filp, int on)
391 {
392         struct drm_file *priv = filp->private_data;
393         struct drm_device *dev = priv->minor->dev;
394
395         DRM_DEBUG("fd = %d, device = 0x%lx\n", fd,
396                   (long)old_encode_dev(priv->minor->device));
397         return fasync_helper(fd, filp, on, &dev->buf_async);
398 }
399 EXPORT_SYMBOL(drm_fasync);
400
401 static void drm_master_release(struct drm_device *dev, struct file *filp)
402 {
403         struct drm_file *file_priv = filp->private_data;
404
405         if (drm_i_have_hw_lock(dev, file_priv)) {
406                 DRM_DEBUG("File %p released, freeing lock for context %d\n",
407                           filp, _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
408                 drm_lock_free(&file_priv->master->lock,
409                               _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
410         }
411 }
412
413 static void drm_events_release(struct drm_file *file_priv)
414 {
415         struct drm_device *dev = file_priv->minor->dev;
416         struct drm_pending_event *e, *et;
417         struct drm_pending_vblank_event *v, *vt;
418         unsigned long flags;
419
420         spin_lock_irqsave(&dev->event_lock, flags);
421
422         /* Remove pending flips */
423         list_for_each_entry_safe(v, vt, &dev->vblank_event_list, base.link)
424                 if (v->base.file_priv == file_priv) {
425                         list_del(&v->base.link);
426                         drm_vblank_put(dev, v->pipe);
427                         v->base.destroy(&v->base);
428                 }
429
430         /* Remove unconsumed events */
431         list_for_each_entry_safe(e, et, &file_priv->event_list, link)
432                 e->destroy(e);
433
434         spin_unlock_irqrestore(&dev->event_lock, flags);
435 }
436
437 /**
438  * Release file.
439  *
440  * \param inode device inode
441  * \param file_priv DRM file private.
442  * \return zero on success or a negative number on failure.
443  *
444  * If the hardware lock is held then free it, and take it again for the kernel
445  * context since it's necessary to reclaim buffers. Unlink the file private
446  * data from its list and free it. Decreases the open count and if it reaches
447  * zero calls drm_lastclose().
448  */
449 int drm_release(struct inode *inode, struct file *filp)
450 {
451         struct drm_file *file_priv = filp->private_data;
452         struct drm_device *dev = file_priv->minor->dev;
453         int retcode = 0;
454
455         mutex_lock(&drm_global_mutex);
456
457         DRM_DEBUG("open_count = %d\n", dev->open_count);
458
459         if (dev->driver->preclose)
460                 dev->driver->preclose(dev, file_priv);
461
462         /* ========================================================
463          * Begin inline drm_release
464          */
465
466         DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
467                   task_pid_nr(current),
468                   (long)old_encode_dev(file_priv->minor->device),
469                   dev->open_count);
470
471         /* Release any auth tokens that might point to this file_priv,
472            (do that under the drm_global_mutex) */
473         if (file_priv->magic)
474                 (void) drm_remove_magic(file_priv->master, file_priv->magic);
475
476         /* if the master has gone away we can't do anything with the lock */
477         if (file_priv->minor->master)
478                 drm_master_release(dev, filp);
479
480         if (drm_core_check_feature(dev, DRIVER_HAVE_DMA))
481                 drm_core_reclaim_buffers(dev, file_priv);
482
483         drm_events_release(file_priv);
484
485         if (dev->driver->driver_features & DRIVER_MODESET)
486                 drm_fb_release(file_priv);
487
488         if (dev->driver->driver_features & DRIVER_GEM)
489                 drm_gem_release(dev, file_priv);
490
491         mutex_lock(&dev->ctxlist_mutex);
492         if (!list_empty(&dev->ctxlist)) {
493                 struct drm_ctx_list *pos, *n;
494
495                 list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
496                         if (pos->tag == file_priv &&
497                             pos->handle != DRM_KERNEL_CONTEXT) {
498                                 if (dev->driver->context_dtor)
499                                         dev->driver->context_dtor(dev,
500                                                                   pos->handle);
501
502                                 drm_ctxbitmap_free(dev, pos->handle);
503
504                                 list_del(&pos->head);
505                                 kfree(pos);
506                                 --dev->ctx_count;
507                         }
508                 }
509         }
510         mutex_unlock(&dev->ctxlist_mutex);
511
512         mutex_lock(&dev->struct_mutex);
513
514         if (file_priv->is_master) {
515                 struct drm_master *master = file_priv->master;
516                 struct drm_file *temp;
517                 list_for_each_entry(temp, &dev->filelist, lhead) {
518                         if ((temp->master == file_priv->master) &&
519                             (temp != file_priv))
520                                 temp->authenticated = 0;
521                 }
522
523                 /**
524                  * Since the master is disappearing, so is the
525                  * possibility to lock.
526                  */
527
528                 if (master->lock.hw_lock) {
529                         if (dev->sigdata.lock == master->lock.hw_lock)
530                                 dev->sigdata.lock = NULL;
531                         master->lock.hw_lock = NULL;
532                         master->lock.file_priv = NULL;
533                         wake_up_interruptible_all(&master->lock.lock_queue);
534                 }
535
536                 if (file_priv->minor->master == file_priv->master) {
537                         /* drop the reference held my the minor */
538                         if (dev->driver->master_drop)
539                                 dev->driver->master_drop(dev, file_priv, true);
540                         drm_master_put(&file_priv->minor->master);
541                 }
542         }
543
544         BUG_ON(dev->dev_mapping == NULL);
545         iput(container_of(dev->dev_mapping, struct inode, i_data));
546
547         /* drop the reference held my the file priv */
548         drm_master_put(&file_priv->master);
549         file_priv->is_master = 0;
550         list_del(&file_priv->lhead);
551         mutex_unlock(&dev->struct_mutex);
552
553         if (dev->driver->postclose)
554                 dev->driver->postclose(dev, file_priv);
555
556         if (drm_core_check_feature(dev, DRIVER_PRIME))
557                 drm_prime_destroy_file_private(&file_priv->prime);
558
559         put_pid(file_priv->pid);
560         kfree(file_priv);
561
562         /* ========================================================
563          * End inline drm_release
564          */
565
566         atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
567         if (!--dev->open_count) {
568                 if (atomic_read(&dev->ioctl_count)) {
569                         DRM_ERROR("Device busy: %d\n",
570                                   atomic_read(&dev->ioctl_count));
571                         retcode = -EBUSY;
572                 } else
573                         retcode = drm_lastclose(dev);
574                 if (drm_device_is_unplugged(dev))
575                         drm_put_dev(dev);
576         }
577         mutex_unlock(&drm_global_mutex);
578
579         return retcode;
580 }
581 EXPORT_SYMBOL(drm_release);
582
583 static bool
584 drm_dequeue_event(struct drm_file *file_priv,
585                   size_t total, size_t max, struct drm_pending_event **out)
586 {
587         struct drm_device *dev = file_priv->minor->dev;
588         struct drm_pending_event *e;
589         unsigned long flags;
590         bool ret = false;
591
592         spin_lock_irqsave(&dev->event_lock, flags);
593
594         *out = NULL;
595         if (list_empty(&file_priv->event_list))
596                 goto out;
597         e = list_first_entry(&file_priv->event_list,
598                              struct drm_pending_event, link);
599         if (e->event->length + total > max)
600                 goto out;
601
602         file_priv->event_space += e->event->length;
603         list_del(&e->link);
604         *out = e;
605         ret = true;
606
607 out:
608         spin_unlock_irqrestore(&dev->event_lock, flags);
609         return ret;
610 }
611
612 ssize_t drm_read(struct file *filp, char __user *buffer,
613                  size_t count, loff_t *offset)
614 {
615         struct drm_file *file_priv = filp->private_data;
616         struct drm_pending_event *e;
617         size_t total;
618         ssize_t ret;
619
620         ret = wait_event_interruptible(file_priv->event_wait,
621                                        !list_empty(&file_priv->event_list));
622         if (ret < 0)
623                 return ret;
624
625         total = 0;
626         while (drm_dequeue_event(file_priv, total, count, &e)) {
627                 if (copy_to_user(buffer + total,
628                                  e->event, e->event->length)) {
629                         total = -EFAULT;
630                         break;
631                 }
632
633                 total += e->event->length;
634                 e->destroy(e);
635         }
636
637         return total;
638 }
639 EXPORT_SYMBOL(drm_read);
640
641 unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
642 {
643         struct drm_file *file_priv = filp->private_data;
644         unsigned int mask = 0;
645
646         poll_wait(filp, &file_priv->event_wait, wait);
647
648         if (!list_empty(&file_priv->event_list))
649                 mask |= POLLIN | POLLRDNORM;
650
651         return mask;
652 }
653 EXPORT_SYMBOL(drm_poll);