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