modesetting-101: rename modeflags, as to avoid conflicts with the xorg definitions
[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->open) {
266                 ret = dev->driver->open(dev, priv);
267                 if (ret < 0)
268                         goto out_free;
269         }
270
271
272         /* if there is no current master make this fd it */
273         mutex_lock(&dev->struct_mutex);
274         if (!priv->minor->master) {
275                 priv->minor->master = drm_get_master(priv->minor);
276                 if (!priv->minor->master) {
277                         ret = -ENOMEM;
278                         goto out_free;
279                 }
280
281                 priv->is_master = 1;
282                 priv->master = priv->minor->master;
283
284                 priv->authenticated = 1;
285                 mutex_unlock(&dev->struct_mutex);
286                 if (dev->driver->master_create) {
287                         ret = dev->driver->master_create(dev, priv->master);
288                         if (ret) {
289                                 drm_put_master(priv->minor->master);
290                                 priv->minor->master = priv->master = NULL;
291                                 mutex_unlock(&dev->struct_mutex);
292                                 goto out_free;
293                         }
294                 }
295         } else {
296                 priv->master = priv->minor->master;
297                 mutex_unlock(&dev->struct_mutex);
298         }
299
300         mutex_lock(&dev->struct_mutex);
301         list_add(&priv->lhead, &dev->filelist);
302         mutex_unlock(&dev->struct_mutex);
303
304 #ifdef __alpha__
305         /*
306          * Default the hose
307          */
308         if (!dev->hose) {
309                 struct pci_dev *pci_dev;
310                 pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
311                 if (pci_dev) {
312                         dev->hose = pci_dev->sysdata;
313                         pci_dev_put(pci_dev);
314                 }
315                 if (!dev->hose) {
316                         struct pci_bus *b = pci_bus_b(pci_root_buses.next);
317                         if (b)
318                                 dev->hose = b->sysdata;
319                 }
320         }
321 #endif
322
323         return 0;
324       out_free:
325         drm_free(priv, sizeof(*priv), DRM_MEM_FILES);
326         filp->private_data = NULL;
327         return ret;
328 }
329
330 /** No-op. */
331 int drm_fasync(int fd, struct file *filp, int on)
332 {
333         struct drm_file *priv = filp->private_data;
334         struct drm_device *dev = priv->minor->dev;
335         int retcode;
336
337         DRM_DEBUG("fd = %d, device = 0x%lx\n", fd,
338                   (long)old_encode_dev(priv->minor->device));
339         retcode = fasync_helper(fd, filp, on, &dev->buf_async);
340         if (retcode < 0)
341                 return retcode;
342         return 0;
343 }
344 EXPORT_SYMBOL(drm_fasync);
345
346 static void drm_object_release(struct file *filp)
347 {
348         struct drm_file *priv = filp->private_data;
349         struct list_head *head;
350         struct drm_ref_object *ref_object;
351         int i;
352
353         /*
354          * Free leftover ref objects created by me. Note that we cannot use
355          * list_for_each() here, as the struct_mutex may be temporarily
356          * released by the remove_() functions, and thus the lists may be
357          * altered.
358          * Also, a drm_remove_ref_object() will not remove it
359          * from the list unless its refcount is 1.
360          */
361
362         head = &priv->refd_objects;
363         while (head->next != head) {
364                 ref_object = list_entry(head->next, struct drm_ref_object, list);
365                 drm_remove_ref_object(priv, ref_object);
366                 head = &priv->refd_objects;
367         }
368
369         for (i = 0; i < _DRM_NO_REF_TYPES; ++i)
370                 drm_ht_remove(&priv->refd_object_hash[i]);
371 }
372
373 /**
374  * Release file.
375  *
376  * \param inode device inode
377  * \param file_priv DRM file private.
378  * \return zero on success or a negative number on failure.
379  *
380  * If the hardware lock is held then free it, and take it again for the kernel
381  * context since it's necessary to reclaim buffers. Unlink the file private
382  * data from its list and free it. Decreases the open count and if it reaches
383  * zero calls drm_lastclose().
384  */
385 int drm_release(struct inode *inode, struct file *filp)
386 {
387         struct drm_file *file_priv = filp->private_data;
388         struct drm_device *dev = file_priv->minor->dev;
389         int retcode = 0;
390
391         lock_kernel();
392
393         DRM_DEBUG("open_count = %d\n", dev->open_count);
394
395         if (dev->driver->preclose)
396                 dev->driver->preclose(dev, file_priv);
397
398         /* ========================================================
399          * Begin inline drm_release
400          */
401
402         DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
403                   current->pid, (long)old_encode_dev(file_priv->minor->device),
404                   dev->open_count);
405
406         /* if the master has gone away we can't do anything with the lock */
407         if (file_priv->minor->master) {
408                 if (dev->driver->reclaim_buffers_locked && file_priv->master->lock.hw_lock) {
409                         if (drm_i_have_hw_lock(dev, file_priv)) {
410                                 dev->driver->reclaim_buffers_locked(dev, file_priv);
411                         } else {
412                                 unsigned long _end=jiffies + 3*DRM_HZ;
413                                 int locked = 0;
414                                 
415                                 drm_idlelock_take(&file_priv->master->lock);
416                                 
417                                 /*
418                                  * Wait for a while.
419                                  */
420                                 
421                                 do{
422                                         spin_lock_bh(&file_priv->master->lock.spinlock);
423                                         locked = file_priv->master->lock.idle_has_lock;
424                                         spin_unlock_bh(&file_priv->master->lock.spinlock);
425                                         if (locked)
426                                                 break;
427                                         schedule();
428                                 } while (!time_after_eq(jiffies, _end));
429                                 
430                                 if (!locked) {
431                                         DRM_ERROR("reclaim_buffers_locked() deadlock. Please rework this\n"
432                                                   "\tdriver to use reclaim_buffers_idlelocked() instead.\n"
433                                                   "\tI will go on reclaiming the buffers anyway.\n");
434                                 }
435                                 
436                                 dev->driver->reclaim_buffers_locked(dev, file_priv);
437                                 drm_idlelock_release(&file_priv->master->lock);
438                         }
439                 }
440
441                 if (dev->driver->reclaim_buffers_idlelocked && file_priv->master->lock.hw_lock) {
442                         
443                         drm_idlelock_take(&file_priv->master->lock);
444                         dev->driver->reclaim_buffers_idlelocked(dev, file_priv);
445                         drm_idlelock_release(&file_priv->master->lock);
446                         
447                 }
448
449
450                 if (drm_i_have_hw_lock(dev, file_priv)) {
451                         DRM_DEBUG("File %p released, freeing lock for context %d\n",
452                                   filp, _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
453                         
454                         drm_lock_free(&file_priv->master->lock,
455                                       _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
456                 }
457                 
458
459                 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
460                     !dev->driver->reclaim_buffers_locked) {
461                         dev->driver->reclaim_buffers(dev, file_priv);
462                 }
463         }
464
465         drm_fasync(-1, filp, 0);
466
467         mutex_lock(&dev->ctxlist_mutex);
468
469         if (!list_empty(&dev->ctxlist)) {
470                 struct drm_ctx_list *pos, *n;
471
472                 list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
473                         if (pos->tag == file_priv &&
474                             pos->handle != DRM_KERNEL_CONTEXT) {
475                                 if (dev->driver->context_dtor)
476                                         dev->driver->context_dtor(dev,
477                                                                   pos->handle);
478
479                                 drm_ctxbitmap_free(dev, pos->handle);
480
481                                 list_del(&pos->head);
482                                 drm_free(pos, sizeof(*pos), DRM_MEM_CTXLIST);
483                                 --dev->ctx_count;
484                         }
485                 }
486         }
487         mutex_unlock(&dev->ctxlist_mutex);
488
489         if (drm_core_check_feature(dev, DRIVER_MODESET))
490                 drm_fb_release(filp);
491
492         if (file_priv->is_master) {
493                 struct drm_file *temp;
494                 list_for_each_entry(temp, &dev->filelist, lhead) {
495                         if ((temp->master == file_priv->master) &&
496                             (temp != file_priv))
497                                 temp->authenticated = 0;
498                 }
499
500                 if (file_priv->minor->master == file_priv->master)
501                         file_priv->minor->master = NULL;
502                 drm_put_master(file_priv->master);
503         }
504
505         file_priv->master = NULL;
506         file_priv->is_master = 0;
507
508         mutex_lock(&dev->struct_mutex);
509         drm_object_release(filp);
510
511         list_del(&file_priv->lhead);
512
513         mutex_unlock(&dev->struct_mutex);
514
515         if (dev->driver->postclose)
516                 dev->driver->postclose(dev, file_priv);
517         drm_free(file_priv, sizeof(*file_priv), DRM_MEM_FILES);
518
519         /* ========================================================
520          * End inline drm_release
521          */
522
523         atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
524         spin_lock(&dev->count_lock);
525         if (!--dev->open_count) {
526                 if (atomic_read(&dev->ioctl_count)) {
527                         DRM_ERROR("Device busy: %d\n",
528                                   atomic_read(&dev->ioctl_count));
529                         spin_unlock(&dev->count_lock);
530                         unlock_kernel();
531                         return -EBUSY;
532                 }
533                 spin_unlock(&dev->count_lock);
534                 unlock_kernel();
535                 return drm_lastclose(dev);
536         }
537         spin_unlock(&dev->count_lock);
538
539         unlock_kernel();
540
541         return retcode;
542 }
543 EXPORT_SYMBOL(drm_release);
544
545 /** No-op. */
546 /* This is to deal with older X servers that believe 0 means data is
547  * available which is not the correct return for a poll function.
548  * This cannot be fixed until the Xserver is fixed. Xserver will need
549  * to set a newer interface version to avoid breaking older Xservers.
550  * Without fixing the Xserver you get: "WaitForSomething(): select: errno=22"
551  * http://freedesktop.org/bugzilla/show_bug.cgi?id=1505 if you try
552  * to return the correct response.
553  */
554 unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
555 {
556         /* return (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM); */
557         return 0;
558 }
559 EXPORT_SYMBOL(drm_poll);