Merge commit 'origin/drm-gem' into modesetting-gem
[platform/upstream/libdrm.git] / linux-core / 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 "drm_sarea.h"
39 #include <linux/poll.h>
40
41 static int drm_open_helper(struct inode *inode, struct file *filp,
42                            struct drm_device * dev);
43
44 static int drm_setup(struct drm_device * dev)
45 {
46         int i;
47         int ret;
48
49         if (dev->driver->firstopen) {
50                 ret = dev->driver->firstopen(dev);
51                 if (ret != 0)
52                         return ret;
53         }
54
55         atomic_set(&dev->ioctl_count, 0);
56         atomic_set(&dev->vma_count, 0);
57         dev->buf_use = 0;
58         atomic_set(&dev->buf_alloc, 0);
59
60         if (drm_core_check_feature(dev, DRIVER_HAVE_DMA)) {
61                 i = drm_dma_setup(dev);
62                 if (i < 0)
63                         return i;
64         }
65
66         for (i = 0; i < ARRAY_SIZE(dev->counts); i++)
67                 atomic_set(&dev->counts[i], 0);
68
69         dev->sigdata.lock = NULL;
70
71         dev->queue_count = 0;
72         dev->queue_reserved = 0;
73         dev->queue_slots = 0;
74         dev->queuelist = NULL;
75         dev->context_flag = 0;
76         dev->interrupt_flag = 0;
77         dev->dma_flag = 0;
78         dev->last_context = 0;
79         dev->last_switch = 0;
80         dev->last_checked = 0;
81         init_waitqueue_head(&dev->context_wait);
82         dev->if_version = 0;
83
84         dev->ctx_start = 0;
85         dev->lck_start = 0;
86
87         dev->buf_async = NULL;
88         init_waitqueue_head(&dev->buf_readers);
89         init_waitqueue_head(&dev->buf_writers);
90
91         DRM_DEBUG("\n");
92
93         /*
94          * The kernel's context could be created here, but is now created
95          * in drm_dma_enqueue.  This is more resource-efficient for
96          * hardware that does not do DMA, but may mean that
97          * drm_select_queue fails between the time the interrupt is
98          * initialized and the time the queues are initialized.
99          */
100
101         return 0;
102 }
103
104 /**
105  * Open file.
106  *
107  * \param inode device inode
108  * \param filp file pointer.
109  * \return zero on success or a negative number on failure.
110  *
111  * Searches the DRM device with the same minor number, calls open_helper(), and
112  * increments the device open count. If the open count was previous at zero,
113  * i.e., it's the first that the device is open, then calls setup().
114  */
115 int drm_open(struct inode *inode, struct file *filp)
116 {
117         struct drm_device *dev = NULL;
118         int minor_id = iminor(inode);
119         struct drm_minor *minor;
120         int retcode = 0;
121
122         minor = idr_find(&drm_minors_idr, minor_id);
123         if (!minor)
124                 return -ENODEV;
125
126         if (!(dev = minor->dev))
127                 return -ENODEV;
128
129         retcode = drm_open_helper(inode, filp, dev);
130         if (!retcode) {
131                 atomic_inc(&dev->counts[_DRM_STAT_OPENS]);
132                 spin_lock(&dev->count_lock);
133                 if (!dev->open_count++) {
134                         spin_unlock(&dev->count_lock);
135                         retcode = drm_setup(dev);
136                         goto out;
137                 }
138                 spin_unlock(&dev->count_lock);
139         }
140
141 out:
142         mutex_lock(&dev->struct_mutex);
143         BUG_ON((dev->dev_mapping != NULL) &&
144                (dev->dev_mapping != inode->i_mapping));
145         if (dev->dev_mapping == NULL)
146                 dev->dev_mapping = inode->i_mapping;
147         mutex_unlock(&dev->struct_mutex);
148
149         return retcode;
150 }
151 EXPORT_SYMBOL(drm_open);
152
153 /**
154  * File \c open operation.
155  *
156  * \param inode device inode.
157  * \param filp file pointer.
158  *
159  * Puts the dev->fops corresponding to the device minor number into
160  * \p filp, call the \c open method, and restore the file operations.
161  */
162 int drm_stub_open(struct inode *inode, struct file *filp)
163 {
164         struct drm_device *dev = NULL;
165         struct drm_minor *minor;
166         int minor_id = iminor(inode);
167         int err = -ENODEV;
168         const struct file_operations *old_fops;
169
170         DRM_DEBUG("\n");
171
172         minor = idr_find(&drm_minors_idr, minor_id);
173         if (!minor)
174                 return -ENODEV;
175         
176         if (!(dev = minor->dev))
177                 return -ENODEV;
178
179         old_fops = filp->f_op;
180         filp->f_op = fops_get(&dev->driver->fops);
181         if (filp->f_op->open && (err = filp->f_op->open(inode, filp))) {
182                 fops_put(filp->f_op);
183                 filp->f_op = fops_get(old_fops);
184         }
185         fops_put(old_fops);
186
187         return err;
188 }
189
190 /**
191  * Check whether DRI will run on this CPU.
192  *
193  * \return non-zero if the DRI will run on this CPU, or zero otherwise.
194  */
195 static int drm_cpu_valid(void)
196 {
197 #if defined(__i386__)
198         if (boot_cpu_data.x86 == 3)
199                 return 0;       /* No cmpxchg on a 386 */
200 #endif
201 #if defined(__sparc__) && !defined(__sparc_v9__)
202         return 0;               /* No cmpxchg before v9 sparc. */
203 #endif
204         return 1;
205 }
206
207 /**
208  * Called whenever a process opens /dev/drm.
209  *
210  * \param inode device inode.
211  * \param filp file pointer.
212  * \param dev device.
213  * \return zero on success or a negative number on failure.
214  *
215  * Creates and initializes a drm_file structure for the file private data in \p
216  * filp and add it into the double linked list in \p dev.
217  */
218 static int drm_open_helper(struct inode *inode, struct file *filp,
219                            struct drm_device * dev)
220 {
221         int minor_id = iminor(inode);
222         struct drm_file *priv;
223         int ret;
224         int i, j;
225
226         if (filp->f_flags & O_EXCL)
227                 return -EBUSY;  /* No exclusive opens */
228         if (!drm_cpu_valid())
229                 return -EINVAL;
230
231         DRM_DEBUG("pid = %d, minor = %d\n", current->pid, minor_id);
232
233         priv = drm_alloc(sizeof(*priv), DRM_MEM_FILES);
234         if (!priv)
235                 return -ENOMEM;
236
237         memset(priv, 0, sizeof(*priv));
238         filp->private_data = priv;
239         priv->filp = filp;
240         priv->uid = current->euid;
241         priv->pid = current->pid;
242         priv->minor = idr_find(&drm_minors_idr, minor_id);
243         priv->ioctl_count = 0;
244         /* for compatibility root is always authenticated */
245         priv->authenticated = capable(CAP_SYS_ADMIN);
246         priv->lock_count = 0;
247
248         INIT_LIST_HEAD(&priv->lhead);
249         INIT_LIST_HEAD(&priv->refd_objects);
250         INIT_LIST_HEAD(&priv->fbs);
251
252         for (i = 0; i < _DRM_NO_REF_TYPES; ++i) {
253                 ret = drm_ht_create(&priv->refd_object_hash[i],
254                                     DRM_FILE_HASH_ORDER);
255                 if (ret)
256                         break;
257         }
258
259         if (ret) {
260                 for (j = 0; j < i; ++j)
261                         drm_ht_remove(&priv->refd_object_hash[j]);
262                 goto out_free;
263         }
264
265         if (dev->driver->driver_features & DRIVER_GEM)
266                 drm_gem_open(dev, priv);
267
268         if (dev->driver->open) {
269                 ret = dev->driver->open(dev, priv);
270                 if (ret < 0)
271                         goto out_free;
272         }
273
274
275         /* if there is no current master make this fd it */
276         mutex_lock(&dev->struct_mutex);
277         if (!priv->minor->master) {
278                 priv->minor->master = drm_get_master(priv->minor);
279                 if (!priv->minor->master) {
280                         ret = -ENOMEM;
281                         goto out_free;
282                 }
283
284                 priv->is_master = 1;
285                 priv->master = priv->minor->master;
286
287                 priv->authenticated = 1;
288                 mutex_unlock(&dev->struct_mutex);
289                 if (dev->driver->master_create) {
290                         ret = dev->driver->master_create(dev, priv->master);
291                         if (ret) {
292                                 drm_put_master(priv->minor->master);
293                                 priv->minor->master = priv->master = NULL;
294                                 mutex_unlock(&dev->struct_mutex);
295                                 goto out_free;
296                         }
297                 }
298         } else {
299                 priv->master = priv->minor->master;
300                 mutex_unlock(&dev->struct_mutex);
301         }
302
303         mutex_lock(&dev->struct_mutex);
304         list_add(&priv->lhead, &dev->filelist);
305         mutex_unlock(&dev->struct_mutex);
306
307 #ifdef __alpha__
308         /*
309          * Default the hose
310          */
311         if (!dev->hose) {
312                 struct pci_dev *pci_dev;
313                 pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
314                 if (pci_dev) {
315                         dev->hose = pci_dev->sysdata;
316                         pci_dev_put(pci_dev);
317                 }
318                 if (!dev->hose) {
319                         struct pci_bus *b = pci_bus_b(pci_root_buses.next);
320                         if (b)
321                                 dev->hose = b->sysdata;
322                 }
323         }
324 #endif
325
326         return 0;
327       out_free:
328         drm_free(priv, sizeof(*priv), DRM_MEM_FILES);
329         filp->private_data = NULL;
330         return ret;
331 }
332
333 /** No-op. */
334 int drm_fasync(int fd, struct file *filp, int on)
335 {
336         struct drm_file *priv = filp->private_data;
337         struct drm_device *dev = priv->minor->dev;
338         int retcode;
339
340         DRM_DEBUG("fd = %d, device = 0x%lx\n", fd,
341                   (long)old_encode_dev(priv->minor->device));
342         retcode = fasync_helper(fd, filp, on, &dev->buf_async);
343         if (retcode < 0)
344                 return retcode;
345         return 0;
346 }
347 EXPORT_SYMBOL(drm_fasync);
348
349 static void drm_object_release(struct file *filp)
350 {
351         struct drm_file *priv = filp->private_data;
352         struct list_head *head;
353         struct drm_ref_object *ref_object;
354         int i;
355
356         /*
357          * Free leftover ref objects created by me. Note that we cannot use
358          * list_for_each() here, as the struct_mutex may be temporarily
359          * released by the remove_() functions, and thus the lists may be
360          * altered.
361          * Also, a drm_remove_ref_object() will not remove it
362          * from the list unless its refcount is 1.
363          */
364
365         head = &priv->refd_objects;
366         while (head->next != head) {
367                 ref_object = list_entry(head->next, struct drm_ref_object, list);
368                 drm_remove_ref_object(priv, ref_object);
369                 head = &priv->refd_objects;
370         }
371
372         for (i = 0; i < _DRM_NO_REF_TYPES; ++i)
373                 drm_ht_remove(&priv->refd_object_hash[i]);
374 }
375
376 /**
377  * Release file.
378  *
379  * \param inode device inode
380  * \param file_priv DRM file private.
381  * \return zero on success or a negative number on failure.
382  *
383  * If the hardware lock is held then free it, and take it again for the kernel
384  * context since it's necessary to reclaim buffers. Unlink the file private
385  * data from its list and free it. Decreases the open count and if it reaches
386  * zero calls drm_lastclose().
387  */
388 int drm_release(struct inode *inode, struct file *filp)
389 {
390         struct drm_file *file_priv = filp->private_data;
391         struct drm_device *dev = file_priv->minor->dev;
392         int retcode = 0;
393
394         lock_kernel();
395
396         DRM_DEBUG("open_count = %d\n", dev->open_count);
397
398         if (dev->driver->preclose)
399                 dev->driver->preclose(dev, file_priv);
400
401         /* ========================================================
402          * Begin inline drm_release
403          */
404
405         DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
406                   current->pid, (long)old_encode_dev(file_priv->minor->device),
407                   dev->open_count);
408
409         /* if the master has gone away we can't do anything with the lock */
410         if (file_priv->minor->master) {
411                 if (dev->driver->reclaim_buffers_locked && file_priv->master->lock.hw_lock) {
412                         if (drm_i_have_hw_lock(dev, file_priv)) {
413                                 dev->driver->reclaim_buffers_locked(dev, file_priv);
414                         } else {
415                                 unsigned long _end=jiffies + 3*DRM_HZ;
416                                 int locked = 0;
417                                 
418                                 drm_idlelock_take(&file_priv->master->lock);
419                                 
420                                 /*
421                                  * Wait for a while.
422                                  */
423                                 
424                                 do{
425                                         spin_lock_bh(&file_priv->master->lock.spinlock);
426                                         locked = file_priv->master->lock.idle_has_lock;
427                                         spin_unlock_bh(&file_priv->master->lock.spinlock);
428                                         if (locked)
429                                                 break;
430                                         schedule();
431                                 } while (!time_after_eq(jiffies, _end));
432                                 
433                                 if (!locked) {
434                                         DRM_ERROR("reclaim_buffers_locked() deadlock. Please rework this\n"
435                                                   "\tdriver to use reclaim_buffers_idlelocked() instead.\n"
436                                                   "\tI will go on reclaiming the buffers anyway.\n");
437                                 }
438                                 
439                                 dev->driver->reclaim_buffers_locked(dev, file_priv);
440                                 drm_idlelock_release(&file_priv->master->lock);
441                         }
442                 }
443
444                 if (dev->driver->reclaim_buffers_idlelocked && file_priv->master->lock.hw_lock) {
445                         
446                         drm_idlelock_take(&file_priv->master->lock);
447                         dev->driver->reclaim_buffers_idlelocked(dev, file_priv);
448                         drm_idlelock_release(&file_priv->master->lock);
449                         
450                 }
451
452
453                 if (drm_i_have_hw_lock(dev, file_priv)) {
454                         DRM_DEBUG("File %p released, freeing lock for context %d\n",
455                                   filp, _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
456                         
457                         drm_lock_free(&file_priv->master->lock,
458                                       _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
459                 }
460                 
461
462                 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
463                     !dev->driver->reclaim_buffers_locked) {
464                         dev->driver->reclaim_buffers(dev, file_priv);
465                 }
466         }
467
468         if (dev->driver->driver_features & DRIVER_GEM)
469                 drm_gem_release(dev, file_priv);
470
471         drm_fasync(-1, filp, 0);
472
473         mutex_lock(&dev->ctxlist_mutex);
474
475         if (!list_empty(&dev->ctxlist)) {
476                 struct drm_ctx_list *pos, *n;
477
478                 list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
479                         if (pos->tag == file_priv &&
480                             pos->handle != DRM_KERNEL_CONTEXT) {
481                                 if (dev->driver->context_dtor)
482                                         dev->driver->context_dtor(dev,
483                                                                   pos->handle);
484
485                                 drm_ctxbitmap_free(dev, pos->handle);
486
487                                 list_del(&pos->head);
488                                 drm_free(pos, sizeof(*pos), DRM_MEM_CTXLIST);
489                                 --dev->ctx_count;
490                         }
491                 }
492         }
493         mutex_unlock(&dev->ctxlist_mutex);
494
495         if (drm_core_check_feature(dev, DRIVER_MODESET))
496                 drm_fb_release(filp);
497
498         if (file_priv->is_master) {
499                 struct drm_file *temp;
500                 list_for_each_entry(temp, &dev->filelist, lhead) {
501                         if ((temp->master == file_priv->master) &&
502                             (temp != file_priv))
503                                 temp->authenticated = 0;
504                 }
505
506                 if (file_priv->minor->master == file_priv->master)
507                         file_priv->minor->master = NULL;
508                 drm_put_master(file_priv->master);
509         }
510
511         file_priv->master = NULL;
512         file_priv->is_master = 0;
513
514         mutex_lock(&dev->struct_mutex);
515         drm_object_release(filp);
516
517         list_del(&file_priv->lhead);
518
519         mutex_unlock(&dev->struct_mutex);
520
521         if (dev->driver->postclose)
522                 dev->driver->postclose(dev, file_priv);
523         drm_free(file_priv, sizeof(*file_priv), DRM_MEM_FILES);
524
525         /* ========================================================
526          * End inline drm_release
527          */
528
529         atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
530         spin_lock(&dev->count_lock);
531         if (!--dev->open_count) {
532                 if (atomic_read(&dev->ioctl_count)) {
533                         DRM_ERROR("Device busy: %d\n",
534                                   atomic_read(&dev->ioctl_count));
535                         spin_unlock(&dev->count_lock);
536                         unlock_kernel();
537                         return -EBUSY;
538                 }
539                 spin_unlock(&dev->count_lock);
540                 unlock_kernel();
541                 return drm_lastclose(dev);
542         }
543         spin_unlock(&dev->count_lock);
544
545         unlock_kernel();
546
547         return retcode;
548 }
549 EXPORT_SYMBOL(drm_release);
550
551 /** No-op. */
552 /* This is to deal with older X servers that believe 0 means data is
553  * available which is not the correct return for a poll function.
554  * This cannot be fixed until the Xserver is fixed. Xserver will need
555  * to set a newer interface version to avoid breaking older Xservers.
556  * Without fixing the Xserver you get: "WaitForSomething(): select: errno=22"
557  * http://freedesktop.org/bugzilla/show_bug.cgi?id=1505 if you try
558  * to return the correct response.
559  */
560 unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
561 {
562         /* return (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM); */
563         return 0;
564 }
565 EXPORT_SYMBOL(drm_poll);